0

I'm trying to be able to add a query to my js file that will change the versions.

For example:

<script type="text/javascript" src="myjavascript.js?v=1.0.0"></script>

I know this is possible because I seen other sites do this.

Thanks for help.

Shawn31313
  • 5,978
  • 4
  • 38
  • 80
  • What exactly is your question? – Nathan Taylor Feb 24 '12 at 02:52
  • You can use javascript to load in a library just by creating a script element. I have never heard of a page using javascript to unload a library first. Maybe you can just call a jquery .remove() on the original element, but I doubt it. I am interested in the answers you get! – Malk Feb 24 '12 at 02:54
  • v is not actually used like a real querystring, it's for bypassing cached js file. – Okan Kocyigit Feb 24 '12 at 02:57
  • @NathanTaylor Im trying to get the js file to change depending on the query. – Shawn31313 Feb 24 '12 at 02:57

2 Answers2

3

Versioning of JS files in this fashion is nothing more than a technique to force a refresh of a particular cached resource on a user's machine. When you append a unique querystring to a URL, it tells the browser that it should not used a cached version of a particular URL and it forces the browser to redownload that file.

Making use of this functionality is as simple as appending a unique value onto the querystring of the JS file's URL.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • But How would I really get it to change. I'm not getting this. – Shawn31313 Feb 24 '12 at 02:58
  • @Shawn31313 you change it in your html or append the javascript include like shown here: http://stackoverflow.com/a/950146/79835 – row1 Feb 24 '12 at 03:04
  • @Shawn31313 The important thing to note here is that the file itself does not change when you append the querystring, but rather, the browser's cache of that file is updated. The value appended to the querystring can be anything and can come from anywhere. At my company, we have a unique build number which we append to the querystring for some of our static files. – Nathan Taylor Feb 24 '12 at 03:14
  • But will it still parse as Javascript? – Shawn31313 Feb 24 '12 at 03:16
  • @Shawn31313 It surely will; the browser doesn't treat the file any differently, just the cache associated with it. It's nothing more than a *trick* to force an update on the client's browser. – Nathan Taylor Feb 24 '12 at 04:12
0

What you're most likely seeing is a server-side language such as ASP, PHP, etc. "swoop in" and handle the change, then render the contents out as if nothing has happened. Basically, if you can relate to PHP:

<?php
  $v = $_GET['v']; // yes I know--shame on me for no validation
  header('Content-Type: text/javascript');

  include('js/myjavascript-'.$v.'.js');

The other purpose is, as Nathan mentioned, for avoiding caching issues. But, if you see the script's contents changing based on this GET variable it's most likely a server-side language doing so nondescript.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200