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?

2 Answers2

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();
})
Krzysiek
  • 98
  • 1
  • 4
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.

Freez
  • 7,208
  • 2
  • 19
  • 29