The CRM Grid – A Microsoft CRM Blog

Greg Owens’ rose-tinted MS-CRM mumblings

Prevent caching of Javascript include files during development

Recommended JavaScript Book!

I'd also like to highly recommend this JavaScript book by Douglas Crockford - it will definitely improve the quality of your JavaScript development experience!

Surely everyone that does their own clientside Javascripting of the MS-CRM interface is already using the technique described by the almighty Ronald Lemmen to link to .js files to speed up the development process, since it removes the need to republish your entity forms over and over after each scripting change. Although unsuited for use in a production environment (for various reasons), this method has undoubtedly saved me hours in development time.

What I couldn’t understand though was why my updates often didn’t take effect, despite updating and saving my script files and even closing my browser. I even looked around for some evidence that I wasn’t the only one on the planet with this problem. Google suggested I was, so I had to find my own solution.

The problem appeared to be that the .js file was being helpfully cached in IE. By clearing my cache and fully reloading the page, I could solve the problem. Still, it was annoying. My preferred solution is a bit ugly but works a treat – when calling the JavaScript file, ensure that the script source is located at a unique URL each time the page loads. I use the getTime() method of the Date object to give a totally unique URL for that moment in time. The harness to load all my JavaScript files now looks like this:

var dt = new Date();
rndURL = "<script src='full_path_to_source_file.js?random='" + dt.getTime() + "' language='JavaScript'>";
st = document.createElement(rndURL);
h = document.getElementsByTagName('head');
h[0].insertAdjacentElement('beforeEnd',st);

Obviously this script is entirely unsupported by Microsoft, but if you happen to become the second person on the planet to experience the same caching issue I did, this just might save you some time.

Don’t forget that the first time you paste this code into your form onLoad event, you’ll need to republish your form, flush the browser cache, restart IIS, turn around three times then hit F5 in IE before the randomising magic works for you.

About these ads

6 Comments»

  James Adams wrote @

A perfect solution to a problem I’ve been having, works perfectly, thanks!

  K Dorken wrote @

Sigh. You have probably saved more of my hair. Thanks.

  Mohamed Sobhy wrote @

i thing you can use
Ctrl+F5 In the IE browser to reload all the Updated Javascript files from the server and Refresh the IE cach for u r website
hope that it helps any one with the same problem

  Simon Hayes wrote @

Genius – wish I’d thought of that !

  Rich wrote @

Sorry for the dumb question – if I load my js during the onload event I am assuming that I can then reference any of the functions in it from onchange events on controls on the form?

  Greg Owens wrote @

That’s right Rich. What I tend to do, however, is to put all of my code in the onLoad as functions and then use the attachEvent() method to assign the function calls to the onchange eventhandlers of my controls. Result is that all javascript for an entity is in a single location.

Are you having problems referencing your functions?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: