The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

JavaScript / Browser Caches

I am working on a Classic ASP application. There is a file called search.asp, which references search.js with:

<script language="JavaScript" src="search.js"></script>

When I do the following, I get a problem.

1. Upload search.js to the server
2. Upload search.asp to the server
3. Go to search.asp in browser
4. Upload new version of search.asp to browser
5. Press enter in address bar to reload the page

After I complete step 5, the browser is still using the old version of search.js.

How do I get the browser to load the new version?

A few solutions I've thought of:

a) Put the script in search_js.asp, put it in a <script> tag, and use <!--#include file="search_js.asp"--> to include it. This has the disadvantage that it must be reloaded every time.

b) Put the version number in the filename of search.js (e. g. search_v1.js), and increment the version number each time a new version is saved, and update the reference to the file. So the reference doesn't have to be changed every time, put it in an included file, with a name like search_js.asp.

c) Use some sort of JavaScript library for this, like Prototype.

Any ideas on how to handle this situation? Is b) a good approach? Or is there some obvious way of handling this that I'm missing?

I noticed that inbox of GMail has a version number in the URL of the JavaScript it references.
Ben Atkin Send private email
Wednesday, October 04, 2006
Fwiw, this page is using statements such as:

<script type="text/javascript" src="form.js?81"> </script>
Christopher Wells Send private email
Wednesday, October 04, 2006
Thanks for pointing that out, Christopher.

I like the query string method a lot better. It will save me from renaming a file, and will make it possible to easily track modifications to the file with SVN.

I'm going to include a file that contains the <script> tag, or a few of them, so I only have to increment the number in the query string in one place. Chances are, this is what the JoS forum software does, probably with the header and footer.
Ben Atkin Send private email
Thursday, October 05, 2006
I'm new to javascript. I just searched the web for info on passing querystrings to javascripts, but came up empty. It that what is going on here? Are there two options for passing arguments to javascripts: script(arg1, agr2) and script.js?arg1&arg2 ? I realize the the JoS passes querystrings to javascripts in the context of "src=" for a script definition, but that makes me think the javascript (form.js) that returns javascripts is run on the server, thus making me more confused.
Josh Send private email
Thursday, October 05, 2006
JavaScript can't know if it is in a file or embeded into a page.
It can read GET parameters of the page but you can't pass arguments to it via file.js?Var=foo
Jan Hančič Send private email
Thursday, October 05, 2006
You can't actually pass arguments, but can achieve the desired effect:
if a file knows what it is called, it can find the dom element that calls it in the page, and parse the src string for arguments- scriptaculous uses this method, to allow you to do
<script ... src="scriptaculous.js?load=builder,effects">...

to load only the modules you want to.
G Jones
Thursday, October 05, 2006
Thanks! That really clears it up for me. I had not realized  that a javascript file would behave the same as javascript embedded in an html page and have access to whatever basic object model the browser provides for a simple page.
Josh Send private email
Thursday, October 05, 2006
Loook at how web sites like Flickr handle this. You'll see that their Javascript is versioned via these querystring params. This works quite well and of course it could be simply a random number not a true version number.
Thursday, October 12, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz