1

I am loading a script using the script embed tag and I want to get the url variables and the loading script name inside the script. Is it possible? For Example,

<script src="test.js?id=12"></script>

Inside test.js is there any way to get the url variable id?

Any help would be appreciated.

Thanks,
Karthik

Karthik
  • 804
  • 4
  • 15
  • 24
  • 2
    see http://stackoverflow.com/questions/710957/how-might-i-get-the-script-filename-from-within-that-script – Vivek Goel Apr 18 '11 at 11:05

4 Answers4

4

Aside from the answers in the linked post, FWIW with Firefox 4 only you can (with caveats); document.currentScript.src which will return the full url, including arguments.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
2



Thanks for all your efforts I have made that working by assigning an id attribute in the script tag and accessed via jQuery,

<script src="test.js?id=12" id="myScript"></script>

var currentScript = $("#myScript").attr('src'); //This will give me my script src

Thanks,
Karthik

Karthik
  • 804
  • 4
  • 15
  • 24
1

If you want to get a variable from the current URL you can use this:

function queryParser(url){
    this.get=function(p){return this.q[p];}
    this.q={};
    this.map=function(url){
        url=url || window.location.search.substring(1);
        var url=url.split('&');
        var part;
        for(var i=0;i<url.length;i++){
            part=url[i].split('=');
            this.q[part[0]]=part[1];
        }
    }
    this.map(url);
}
var query=new queryParser();
// assuming you have ?test=something
alert(query.get('test'));

I recommend you map the result, so you don't re-parse whenever you want to find a specific element.

I don't really know why you'd pass a query string in a script tag like that, unless you specifically want off-site includes with a simple robust system for various effects. Or are actually using PHP to handle that request.

If you want to "send" a variable to one of your scripts, you can always do:

<script type="text/javascript">
var myVar="I'm in global scope, all scripts can access me";
</script>
<script src="test.js?id=12"></script>

If you really need to get the URL of the currently included script, you can use the code supplied by my peers in the other answers, you can then use:

var query=new queryParser(scriptURL);
alert(query.get('id'));// would alert 12 in your case
Khez
  • 10,172
  • 2
  • 31
  • 51
0

Navigating through the link on your comments you can get the proper answer. Anyway, to make things easier:

var allScripts=document.getElementsByTagName('script');
var indexLastScript= allScripts.length -1;
alert (allScripts[indexLastScript].src);

This will show up "test.js?id=12" as a regular String so its up to you to split it in order to get de param.

Hope it helps, I've tried it on the run over the Chrome Javascript Console. :D.

Ivan Arrizabalaga
  • 686
  • 1
  • 7
  • 22
  • I know about this solution but my problem is I will put this script in host website and based the id it will load some data from my server and not from the host server. Its simply like loading a widget script.So I wont have assurance like in which position the host will embed my script.Any better ideas?Any How thanks for all you guys! – Karthik Apr 18 '11 at 11:58
  • @Karthik: when your script runs, the last script element will be the script that's currently running (except in some edge case). – Marcel Korpel Apr 18 '11 at 12:48