1

newbie here. I've made a simple form in the index.html as you can see below:

<form name="parameters" METHOD="GET" ACTION="pag1.html">
login: <INPUT TYPE="text" NAME="login"> 
password: <INPUT TYPE="text" NAME="password"> 
<input type="submit" value="ok">
</form>

On pag1.html i've placed my javascript to split the URL, as you can see below:

<head>
<script language="JavaScript">
function processUser()
  {
    var parameters = location.search.substring(1).split("&");

var temp = parameters[0].split("=");
l = unescape(temp[1]);
temp = parameters[1].split("=");
p = unescape(temp[1]);
document.getElementById("log").innerHTML = l;
document.getElementById("pass").innerHTML = p;
  }
</script>
</head>

And two DIVs to display both of them:

<div id="log">LOGIN:</div>
<div id="pass">PASSWORD:</div>

The URL on pag1.html is like: " pag1.html?login=123&password=asd "

But all that i get is :
LOGIN:
PASSWORD:

Where did i go wrong?

3
  • 3
    use <input type="password"> for passwords, and never send forms via GET. Commented Mar 21, 2015 at 18:06
  • possible duplicate of How can I get query string values in JavaScript? Commented Mar 21, 2015 at 18:06
  • 1
    There is a chance that the JS is running before the divs are rendered. Commented Mar 21, 2015 at 18:06

2 Answers 2

2

Your function should be called after whole HTML is loaded. To do that you need to place your function as a callback of DOMContentLoaded event.

document.addEventListener("DOMContentLoaded", function(){
    function processUser()
    {
         var parameters = location.search.substring(1).split("&");

         var temp = parameters[0].split("=");
         l = unescape(temp[1]);
         temp = parameters[1].split("=");
         p = unescape(temp[1]);
         document.getElementById("log").innerHTML = l;
         document.getElementById("pass").innerHTML = p;
    }

    processUser();
})
Sign up to request clarification or add additional context in comments.

Comments

1

It's just because your code is executed before #log and #pass rendering in the DOM. Just put your before the closing tag and it will works.

3 Comments

Still didn't get it. Could you please be more accurate?
Try to move your script from the <head> element to the end of your <body> element.
don't forget to call your function processUser(); at the end of your script

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.