0

Possible Duplicate:
How can I get query string values?

Lets say, we have a <script> tag like this in the HTML markup

<script src="/path/file.js?test=1&data=2"></script>

..is there any way to access the query-string within file.js ? window.location does not change of course, so you can't use that for that.

If this is not possible, how can you pass data into a script file ?

Community
  • 1
  • 1
Andre Meinhold
  • 5,087
  • 3
  • 21
  • 29
  • 1
    You can give the script tag an id, and it to get access to the script element. – Musa Sep 19 '12 at 23:45
  • 1
    Answered in another post: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – sharvell Sep 19 '12 at 23:56
  • This has been asked and answered so many times on here. It would've been quicker for you to search than type your question. – Lee Taylor Sep 20 '12 at 00:03
  • @sharvell: that is a totally different question there. LeeTaylor: It would have been less embarrassing for you reading and understanding different kinds of questions. – Andre Meinhold Sep 20 '12 at 00:36

5 Answers5

3

Musa has the right approach here (from the comments). You can access the script tag like any other (you can use an ID to make it easy):

<script id='my_script_1' src="/path/file.js?test=1&data=2"></script>

Then just parse out the src and grab only the query string:

var js_lib = document.getElementById('my_script_1');
var js_url = js_lib.src;
var js_url_pieces = js_url.split('?');
alert(js_url_pieces[1]);

Note: I'm using split for simplicity's sake. You might want to use regex.

If you want reload the js file, just reset the source:

js_lib.src = js_url_pieces[0]+'?new query string';

I think that should work in most browsers.

Alternately, as others have mentioned, you might want to write more flexible functions or use global variables to achieve what you're trying to do through _GET vars.

Community
  • 1
  • 1
Ben D
  • 14,321
  • 3
  • 45
  • 59
0

The only reliable way I can think of is via document.currentScript but that is currently supported only by Firefox.

This question may have some useful information.

To pass data into a script file, I may use global variables. I don't really like them in general but this may be an appropriate use for them.

Community
  • 1
  • 1
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
0

It sounds like you want to pass some data from the parent document, into your included javascript file. You could do it with a global variable, like so

your-file.html:

<script>
var myVar = "Hi";
</script>
<script src="script.js"></script>

script.js:

alert(myVar);
sqren
  • 22,833
  • 7
  • 52
  • 36
0

When the script runs, that script is the last element on the page (due to JavaScript being blocking). You can take advantage of this:

var scripts = document.getElementsByTagName('script'),
    me = scripts[scripts.length-1],
    myurl = me.getAttribute("src"),
    myquery = myurl.split("?")[1].split("#")[0].split("&"),
    qcount = myquery.length, query = {}, i, tmp;
for( i=0; i<qcount; i++) {
    tmp = myquery[i].split("=");
    query[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp[1]);
}

Note that this will only work if it's in the outermost scope of the code, ie. not in a function. It also won't work for async or deferred scripts.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

I am done.

var scripts = document.getElementsByTagName('script');

function getQueryFromFile(filename){
   for(let i of scripts){
      if(i.src.includes(filename)) 
        return i.src.split('?')[1]
   }
}

let res = getQueryFromFile('script.js')
console.log(res) // query=212
<script src="script.js?query=212"></script>
perona chan
  • 101
  • 1
  • 8