views:

203

answers:

6

I observed chunks like below sometimes on web pages. So i am curious to know what this really does? or why its written in such a way?

<script src="somefile.js?param1=one&param2=two" />

i can only make out following few intentions behind it

  • Its not page URL (i mean .aspx/.php/.jsp etc.) so its not hacking kind of code where user can add code like this to pass data without getting users attention as its tag which does not render on UI OR implementing old type of AJAX alternative
  • This kind of URL param are useful if user do not wish the JS file (any other resource like image) to get cached. This can be quick way to manage caching

But i am unable to figure out following

  • Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?
  • Do these parameters have any extra role to play here ?
  • What are the other possible practical scenarios where code like this can be/is used?

So please provide some inputs related with the same

Thanks,

+7  A: 

Running Non-JS Code within .js Extensions

In cases like this, that source .js file might (given proper server-configurations) actually have PHP/.NET code within it, which can read those appended values.

As you said, Avoiding Cache...

Additionally, people will at times append a random string at the end of their referenced elements to avoid loading cached data.

Jonathan Sampson
+2  A: 

It can be used for three different reasons:

1) To generate the javascript file in the server depending on the parameters;

2) To avoid caching;

3) To pass parameters to javacript itself

jbochi
+4  A: 

Either the javascript file is not static (it is generated by the server based on the parameters in its querystring)

OR

In the JavaScript file itself, you can have it check its own querystring parameters (not just that of the page, but that of the javascript source url).

OR

(This doesn't exactly match your scenario, but) you can also add parameters at the end of image and script urls as a way of versioning. The version with the url="somescript.js?V=3" will be cached by the user until the page then changes and the url is not="somescript.js?V=4". The file will be replaced by the version on the server no matter what the browser setting may be.

My guess (without looking at this specific case) is that the javascript file is reading its own querystring. I have done this, and its very helpful.

Gabriel McAdams
You can also add timestamps as parameters to prevent client-side javascript caching (this can help in some specific scenarios).
Russell
A: 

An example of this in practice would be a server side handler for somefile.js that uses the parameters (names of other scripts) to determine which scripts are actually required and combine/minify them, returning them as a single somefile.js script file.

Chris
+2  A: 

Looks like page URL parameters but are these parameters anyway readable in JavaScript file and have some additional utility?

Yes you can read them in JavaScript, Scriptaculous uses that approach for loading modules, eg:

<script type="text/javascript" src="scriptaculous.js?load=effects,dragdrop">
</script>

Do these parameters have any extra role to play here ? What are the other possible practical scenarios where code like this can be/is used?

That can be also used for server-side script joining and minifying, of course using some url rewriting technique to have the .js extension, and as you say, it's a common technique to add timestamp parameters to break the browser cache.

CMS
+4  A: 

The URL having '.js' means nothing. It could still be handled by a server-side script like an ASP or PHP.

Nicolás