.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, Link to comment https://forums.phpfreaks.com/topic/54294-simple-help-with-variables/ 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 Link to comment https://forums.phpfreaks.com/topic/54294-simple-help-with-variables/#findComment-268505 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> Link to comment https://forums.phpfreaks.com/topic/54294-simple-help-with-variables/#findComment-268841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.