[Javascript] Auto reloading JS files on change?

Scott Reynen scott at randomchaos.com
Mon Jun 2 15:14:18 CDT 2008


On [Jun 2], at [ Jun 2] 1:19 , Roger Roelofs wrote:

> Mark,
>
> On Jun 2, 2008, at 8:52 AM, Rees, Mark wrote:
>
>> I have the following in an HTML file that is
>> echo'd to the browser via PHP code.
>>
>> <script src="ajax/players.js"
>> type="text/javascript"></script>
>>
>> But whenever this, and other, .js files are
>> modified the user must know to hit the refresh
>> button on his browser.
>>
>> I've been looking for a way to reload the JS files
>> on changes, and I've come across the last-modified
>> header element, but not sure how to implement this.
>
> Have you looked at JSONP?
> http://simonwillison.net/2005/Dec/16/json/
>
> It basically works by letting you specify a function for the inserted
> script to call so you can know when it has loaded and process any data
> it contains.  The recent version of jQuery has support built in for
> this.

Executing external JavaScript loaded via AJAX is an awfully convoluted  
way to avoid caching.  Simply changing the name of the JavaScript file  
will force a new download by avoiding the cached version associated  
with the original name.  If you want to learn how caching works,  
here's a decent tutorial:

http://www.mnot.net/cache_docs/

Peace,
Scott




More information about the Javascript mailing list