Javascript - pulling a parameter into a text input

Associate
Joined
26 Nov 2002
Posts
859
Hi guys,

I'm urgently looking for some javascript that will pull a parameter from a URL into a text input. It's a normal paramter value, ...html?email=[email protected]

I need to pull out the email address. I've tried google but can't find the exact code that I need. I havent touched javascript or much programming at all so will need something as easy as changing a variable name to work if possible!

Thanks in advance.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Here is a simple example of using querystring variables in javascript:

Code:
<script>
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}
</script>

Now make a request to page.html?x=Hello

<script>
  alert( getQueryVariable("x") );
</script>
taken from : http://www.activsoftware.com/code_s...ript/Get_Query_String_variables_in_JavaScript

In your instance, you would need something akin to:
Code:
<html>
<head>
<script>
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}

function bodyLoad ()
{
    var textBox = document.getElementByID("mytextbox");
    textBox.value = getQueryVariable("var");
}
</script>
</head>
<body onload="bodyLoad()">
<form action="" method="post">
<input type="text" size="10" id="mytextbox" />
</form>
</body>
</html>
 
Back
Top Bottom