4

I include javascript with params:

<script type="text/javascript" src="js/app.min.js?ver=1.13"></script>

How can i get value of variable ver from this script?

4
  • i dont think this is possible. maybe put another script tag infront with the version: <script>var version = '1.13';</script> Commented Mar 2, 2013 at 14:20
  • If it's a global variable in that script, you can just call it, after the script has been included. Commented Mar 2, 2013 at 14:20
  • @Chiel92 impossible $(document).ready(function(){console.log(ver)}); ReferenceError: ver is not defined Commented Mar 2, 2013 at 14:24
  • And of this one Commented Mar 2, 2013 at 14:31

1 Answer 1

7

Try this:
Add an id to script tag:

<script type="text/javascript" src="js/app.min.js?ver=1.13" id="script_id"></script>

and then:

var src=document.getElementById("script_id").src;
alert(src.split("ver=")[1]);

Note that I assumed there is one parameter and this parameter is ver.

Sign up to request clarification or add additional context in comments.

5 Comments

You may solve it even without an id using this: document.querySelectorAll('script[src^='js/app.min']')
Hmm good point :). I think your solution is much better than mine.
While it may not be quite as elegant, selecting by id would be much more efficient.
Accessing script tag by using id must be faster than traversing all script tags and comparing src attribute with 'js/app.min'. But if there aren't lots of script tags, then using querySelectorAll shouldn't be a problem.
Id would be the fastest way - no argument there. But maybe it's not possible for the OP to change the DOM. It was just an addition to show another way and not to say that one is better over the other.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.