[英]How can I pass a value from one HTML page to another using JavaScript?
This is my first HTML page: 这是我的第一个HTML页面:
<!--first.html-->
<html>
<body>
<div data-role="page" data-theme="a" data-url="first" id="first">
<form id="form1" name="form2" action="checking.html">
<input type="text" name="txtFileName" id="txtFileName"/>
<!-- <button onClick="uploadFile();">Upload</button> -->
<input type="hidden" name="hidden1" value="">
<br><input type="submit" value="Send me your name!" onClick="submitform();"><br>
</form>
<script type="text/javascript">
function submitform()
{
document.forms.form1.hidden1.value=1;
alert("i am working");
document.form1.submit();
}
</script>
</div>
</body>
</html>
This is my second HTML page: 这是我的第二个HTML页面:
<!-- second.html -->
<html>
<head>
</head>
<body>
<h1>Javascript call after page loaded</h1>
<script>
function getQueryVariable2(variable) {
var query = window.location.search.substring(1);
document.write(query);
var vars = query.split("&");
document.write("<br />");
document.write(vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
document.write("<br />txtFileName = " + getQueryVariable2("txtFileName"));
document.write("<br />hid1 = " + getQueryVariable2("hid1"));
</script>
hellllo
</body>
Here I want to display the contents of hidden1 from first.html. 在这里,我想从first.html显示hidden1的内容。 Please suggest to me what code I should use for this.
请告诉我我应该使用哪些代码。
Probably the best way in your case to use GET params like: 可能是您使用GET参数的最佳方式,例如:
http://mysite//second.html?myparams=value
or if it's important or big data - use POST 或者如果它是重要的或大数据 - 使用POST
Found a solution for you to parse GET variables: 找到解析GET变量的解决方案:
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
If you are not using html5, you have these ways to pass the value from one html to another - QueryString/GET/Cookies. 如果你没有使用html5,你可以通过这些方法将值从一个html传递给另一个html - QueryString / GET / Cookies。
HTML5 provides two objects localStorage and sessionStorage to save the client data. HTML5提供了两个对象localStorage和sessionStorage来保存客户端数据。 Both allow user to store the data on local machine.
两者都允许用户将数据存储在本地计算机上。 Provides two methods - getItem('Key') and setItem('Key','Value') or we can just store the data in array of localStorage or sesionStorage;
提供两种方法 - getItem('Key')和setItem('Key','Value')或者我们可以将数据存储在localStorage或sesionStorage的数组中;
// Store
localStorage.setItem("lastname", "abc");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
The sessionStorage object is similar to the localStorage object except it stores the data for only one session. sessionStorage对象类似于localStorage对象,除了它仅存储一个会话的数据。 The data is deleted when user closes the window.
用户关闭窗口时删除数据。
to remove any item from session: 从会话中删除任何项目:
localStorage.removeItem("lastname");
to store as an array: 存储为数组:
for (item in items) {
localStorage[item] = AnyArray[item];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.