
javascript to pass value to other html file

1)values.html:
<html>
<script>
function send(){
var f=document.form;
var fname=f.firstname.value;
var lname=f.lastname.value;
window.location.replace("passvalues.html?f="+fname+"&&l="+lname);
}
</script>
<form name="form" action="passvalues.html">
<table>
<tr>
<td>First Name:</td>
<td><input type=text name=firstname size=10></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type=text name=lastname size=10></td>
</tr>
<tr>
<td colspan=2><input type=button value="Submit" onclick="send();">
</td>
</tr>
</table>
</form>
</html>
2)passvalues.html:
<html>
<script>
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
</script>
<script>
firstname = unescape(params["f"]);
lastname = unescape(params["l"]);
document.write("Firstname = " + firstname + "<br>");
document.write("Lastname = " + lastname + "<br>");
</script>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.