Help with javascript - alert not displaying

Help with javascript - alert not displaying
0

#1

Does anyone know how can I resolve the following: I want the form input by the user, to be alerted in a different HTML file.

This is what I wrote so far. It works for the id called ‘name’, but I dont know why it does not for the ‘position’.

“index.html” file:

<script>
	function myFunction() {
        name = document.getElementById("name").value;
    	position = document.getElementById("position").value;
    	
    	window.location.replace("signature.html");
    	return false;
	}
</script>

<form class="formulario" onsubmit="return myFunction()">
	  Name: <input type="text" name="name" id="name"><br>
	  Position:  <input type="text" name="position" id="position"><br>
	  <br>
	  <input type="submit" value="Generate Signature">
</form> 

“signature.html” file - notice the alert(name) and the alert(position) => the alert(name) works, but not the alert(position). I want to understand how to fix that please.

<html>
<head>
</head>
<body>
<div class="signature_general">
	<div class = "signature_middle">
		<div id = "signature_details">
		<font size="2px">Regards,</font><br>
		<b><font color="#808080" size="3px" id="nameInput">Francisco Jurado</font></b><br>
		<font size="2px" id="posInput">Accounting Systems Team Leader</font>
		</div>
	</div>
</div>

<script>
	alert(name);
	alert(position);
</script>

</body>
</html>

Thanks very much


#2

I think that when you use window.location.replace, you are in fact loading a new page (without the back history) and loose every variable that were declared before.

As for the fact that you can maintain the ‘name’ value, it might be because you are renaming the ‘windows.name’ propriety of the windows (tab…): Window name Property.
To test it, you can enter ‘window.name’ in the browser console or just ‘name’.

It should happen like this because you are not declaring the variables with var. So the browser assumes that you are trying to change an existing property/object, since it doesn’t have a variable ‘name’.

If you want, you can pass your variables values as URL parameters to a new windows. See this stackoverflow issue for more about it: Additional parameters in window.location.replace