Robert DeBoer
Robert DeBoer

Reputation: 1695

What is a "?" for in the src attribute of a html script tag?

If this has been asked before, I apologize but this is kinda of a hard question to search for. This is the first time I have come across this in all my years of web development, so I'm pretty curious.

I am editing some HTML files for a website, and I have noticed that in the src attribute of the script tags that the previous author appended a question mark followed by data.

Ex: <script src="./js/somefile.js?version=3.2"></script>

I know that this is used in some languages for value passing in GET request, such as PHP, but as I far as I ever knew, this wasn't done in javascript - at least in calling a javascript file. Does anyone know what this does, if anything?

EDIT: Wow, a lot of responses. Thanks one and all. And since a lot of people are saying similar things, I will post an global update instead of commenting everyone.

In this case the javascript files are static, hence my curiosity. I have also opened them up and did not see anything attempt to access variables on file load. I've never thought about caching or plain version control, both which seam more likely in this circumstance.

Upvotes: 8

Views: 5670

Answers (8)

Zac Bowling
Zac Bowling

Reputation: 6578

The query string has nothing to do with the javascript. Some server side code is hosting up a different version depending on that querystring it appears.

You should never assume anything about paths in a URL. The extension on a path in a URL doesn't really tell you anything. URLs can be completely dynamic and served by some server side code or can rewritten in web servers dynamically.

Now it is common to add a querystring to urls when loading javascript files to prevent client side caching. If the page updates and references a new version of the script then the page can bust through and cause the client to refresh it's script.

Upvotes: 1

Phrogz
Phrogz

Reputation: 303208

A (well-configured) web server will send static files like JavaScript source code once and tell the web browser to cache that file locally for a certain period of time (could be a day, a week, a month, or longer). When the browser sees another request for that same file, it will just use that version instead of getting new code from the server.

If the URL changes -- for example by adding a query string -- then the browser suspects that its cached version is no good and gets a new one. As such, the ? helps developers say "Oops, I changed this file, make sure the browser gets a new copy."

Upvotes: 2

cdhowie
cdhowie

Reputation: 168988

My guess is it's so if he publishes a new version of the JavaScript file, he can bump the version in the HTML documents. This will not do anything server-side when requested, but it causes the browser to treat it as a different file, effectively forcing the browser to re-fetch the script and bypass the local cache of the file.

This way, you can set a really high cache time (like a week or a month!) but not sacrifice the ability to update scripts frequently if necessary.

Upvotes: 7

Adrian
Adrian

Reputation: 2923

I believe what the author was doing was ensuring that if he creates version 3.3 of his script he can change the version= in the url of the script to ensure that users download the new file instead of running off of the old script cached in their browser.

So in this case it is part of the caching strategy.

Upvotes: 9

erjiang
erjiang

Reputation: 45657

Just because the file extension is .js doesn't mean that the target is an actual .js file. They could set up their web server to pass the requested URL to a script (or literally have a script named somefile.js) and have that interpret the filename and version.

Upvotes: 1

Ray
Ray

Reputation: 21905

this is used to guarantee that the browser downloads a new version of the script when available. The version number in the url is incremented each time a new version is deployed so that the browser see it as a different file.

Upvotes: 1

ocodo
ocodo

Reputation: 30248

In this case it's probably being used to ensure the source file isn't cached between versions.

Of course, it could also be used server side to generate the javascript file, without knowing what you have on the other end of the request, it's difficult to be definitive.

BTW, the ?... portion of the url is called the query string.

Upvotes: 1

kemiller2002
kemiller2002

Reputation: 115440

What you have to remember is that this ./js/somefile.js?version=3.2 doesn't have to be a physical file. It can be a page which creates the file on the fly. So you could have it where the request says, "Hey give me version 3 of this js file," and the server side code creates it and writes it to the output stream.

The other option is to force the browser to not cache the file and pull down the new one when it makes the request. Since the URI changed, it will think the file is completely new.

Upvotes: 5

Related Questions