2

Often, when I update a JavaScript or CSS file used by a webpage, I will tack on v=<version>. This helps me track what changes I have made but also forces the browser to get the newest version instead of using cache. Example:

<script src="functions.js?v=1.4"></script>

Is is possible, using JavaScript, to detect any query parameters used? Meaning, could function.js detect that v is 1.4?

4
  • Well you could parse the version out of the href Commented Feb 28, 2014 at 20:11
  • This is a duplicate of stackoverflow.com/questions/7992354/… Look at this page to get the answer. Commented Feb 28, 2014 at 20:15
  • It's duplicate question: stackoverflow.com/questions/2976651/… Commented Feb 28, 2014 at 20:16
  • @EAndreyF, not exactly. If the question is about can the script get its own query params, it's not. Commented Feb 28, 2014 at 20:20

2 Answers 2

4

There's no a specific method for this, but you can inspect the DOM as usual:

var script = document.querySelector('script[src~="function.js"]');
script.src.replace(/.+?v=/, ''); // that's your version
Sign up to request clarification or add additional context in comments.

5 Comments

And then you'll need to apply some helper to get query parameter value you'd like
Think this can be approved as the right one. Also it should be tested, whether it could be done from the same script. I think you need a separate one.
document.querySelector('script[src~="function.js"]') seems to return null for me, although, for the time being, I was able to loop over document.querySelectorAll('script') to manually find my file
@steveo225 Try src~="functions.js" (with s) instead of src~="function.js".
I did make sure the filename was correct. In actuality, the file isn't even named functions.js, that was just an example. Not sure why it reports null, but like I said, I brute forced it with querySelectorAll
0
<script id="file-js" src="functions.js?v=1.4"></script>

Code to find all parameters :

var url = document.getElementById('file-js').src;
url = url.replace(/^.*?\?/, '');
var params = url.split('&');
for(var i in params) {
    var pair = params[i].split('=');
    console.log(pair);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.