Jump to content

Simple Help with Variables


.Darkman

Recommended Posts

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

<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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.