如何将值从一个 html 页面传递到另一个 html 页面 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8550833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Pass values form One html Page to Another html Page javascript?
提问by Raj
How can I pass values from one html page to another html page javascript
如何将值从一个 html 页面传递到另一个 html 页面 javascript
For example:
例如:
Page1.html page
Page1.html 页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>arasu</title>
<style type="text/css">
#popupbox{
margin: 0;
margin-left: 40%;
margin-right: 40%;
margin-top: 50px;
padding-top: 10px;
width: 20%;
height: 150px;
position: absolute;
background: #FBFBF0;
border: solid #000000 2px;
z-index: 9;
font-family: arial;
visibility: hidden;
}
</style>
<script language="JavaScript" type="text/javascript">
function login(showhide){
if(showhide == "show"){
document.getElementById('popupbox').style.visibility="visible";
}else if(showhide == "hide"){
document.getElementById('popupbox').style.visibility="hidden";
}
}
</script>
</head>
<body>
<div >
<form name="login" action="AgaramAnimation.html" method="get">
<center>Username:</center>
<center><input name="username" size="14" /></center>
<center>Password:</center>
<center><input name="password" type="password" size="14" /></center>
<center><input type="submit" name="submit" value="login" /></center>
</form>
</div>
</body>
</html>
here i enter two values user name and password when i click submit button the popup will closed and values pass to another page java script given below .
在这里,当我单击提交按钮时,我输入两个值用户名和密码,弹出窗口将关闭并且值传递到下面给出的另一个页面 java 脚本。
Page2.html
页面2.html
function gettingvalues()
{
var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "Data Source="";Initial Catalog="";User ID="";Password="";Provider=""";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("select * from logindetails where username='" **+ username +** "' and password= '" **+ password +** "'", connection);
if (!rs.eof) {
document.getElementById("").innerHTML = ""; }
else {
alert('please enter valid username and password');
}
connection.close;
}
Pls help me..........
请帮助我..........
回答by David Gilbertson
Assuming your two pages are on the same domain, you can use localStorage.
假设您的两个页面在同一个域中,您可以使用 localStorage。
Popup.html:
弹出窗口.html:
var user = prompt('user name', '');
var password = prompt('password', '');
localStorage.user = user;
localStorage.password = password;
Then back in the main page:
然后回到主页面:
var user = localStorage.user;
var password = localStorage.password;
Note 1: I won't comment on security (other than to say of course don't do it like I've done it above!)
注 1:我不会评论安全性(除了说当然不要像我上面那样做!)
Note 2: localStorage is only about 92% supported: http://caniuse.com/namevalue-storage
注 2:localStorage 仅支持约 92%:http: //caniuse.com/namevalue-storage
回答by JB Nizet
If Main.html
has opened Popup.html
with window.open()
, then the Popup.html page can have a reference to its opening window (the window of Main.html
) using window.opener
. It could thus call a setCredentials
callback function defined in Main.html
using
如果Main.html
已开通Popup.html
使用window.open()
,那么Popup.html页面都可以有它的开放窗口的引用(窗口Main.html
用)window.opener
。因此,它可以调用setCredentials
在Main.html
using 中定义的回调函数
window.opener.setCredentials(...);
Example:
例子:
in Main.html:
在 Main.html 中:
// callback function called by the popup
function setCredentials(login, password) {
// do what you want with the login and password
}
in Popup.html
在 Popup.html 中
// function called when the form is submitted
function login(login, password) {
window.opener.setCredentials(login, password);
window.close();
}
回答by vassilo
You can refer to the window that opened your new window (called the parent window) via window.parent
and then execute your javascript using that object.
您可以通过引用打开新窗口(称为父窗口)的窗口window.parent
,然后使用该对象执行 javascript。
Here is an example of forcing a parent page to refresh using this: [1]Forcing parent page refresh through javascript
这是使用此强制刷新父页面的示例:[1]Forcing parent page refresh through javascript
So in your case you could have say variables for username and password in your js like this and you could set them in the popup window like so:
因此,在您的情况下,您可以像这样在 js 中说出用户名和密码的变量,并且可以像这样在弹出窗口中设置它们:
//parent window
username = "";
password = "";
....
//popup window
window.parent.username = enteredUsername;
window.parent.password = enteredPassword;