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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.