.Darkman Posted June 5, 2007 Share Posted June 5, 2007 Hello everybody, I need some small help with GET Variables in JS. I'll explain this with an example. In an HTML file, i have the following code : <script type="javascript" src="http://anothersite.com/file.js?name=Darkman"></script> The JS file is as follows : document.write("The name is '+name+'"); Now, how do i detect and store this "Darkman" in the "name" variable ? Thanks, Quote Link to comment Share on other sites More sharing options...
paul2463 Posted June 5, 2007 Share Posted June 5, 2007 <script type="text/javascript"> var url = String(window.location); var index = url.indexOf("?"); var data = url.substr(index+1); var splitted = data.split("="); var getName = splitted[0]; var getVar = splitted[1]; </script> getName will have the variable name in you case NAME getVar will have the variable value in your case Darkman WARNING ***** UN TESTED CODE ******WARNING Quote Link to comment Share on other sites More sharing options...
nogray Posted June 5, 2007 Share Posted June 5, 2007 Other option is to set the name variable before hand <script language="javascript"> var name = "darkman"; </script> <script type="javascript" src="http://anothersite.com/file.js"></script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.