2011-12-18 84 views
6

如何传递从一个HTML页面值的JavaScript如何将值形式传递给另一个html页面一个html页面javascript?

例如另一个html页面:

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> 

这里我输入两个值的用户名和密码时,我点击提交按钮弹出将关闭和值传递给另一页下面给出的Java脚本。

Page2.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; 
     } 

请帮我..........

回答

0

如果Main.html开辟Popup.htmlwindow.open(),那么Popup.html页面可以有使用window.opener参考其开放窗口(窗口Main.html)。因此,它可以调用使用Main.html定义的setCredentials回调函数

window.opener.setCredentials(...); 

例子:

在main.html中

// callback function called by the popup 
function setCredentials(login, password) { 
    // do what you want with the login and password 
} 
在Popup.html

// function called when the form is submitted 
function login(login, password) { 
    window.opener.setCredentials(login, password); 
    window.close(); 
} 
+0

请举一些例子。 – Raj 2011-12-18 09:36:51

+0

很难用像jsfiddle这样的网页编辑器来制作类似的例子,因为它需要单独的html页面才能工作。 – keithwyland 2013-04-24 23:18:32

0

你可以参考打开新窗口(称为父窗口)的窗口window.parent,然后使用该对象执行你的JavaScript。

这里是强制父页面使用刷新的一个例子是:[1]Forcing parent page refresh through javascript

所以你的情况你可能会说,在你的js的用户名和密码变量,这样,你可以将它们设置在弹出的窗口像这样:

//parent window 
username = ""; 
password = ""; 

.... 

//popup window 
window.parent.username = enteredUsername; 
window.parent.password = enteredPassword; 
+0

弹出式窗口是独立的Html.Page。这里是我的问题,当我点击main.html中的链接,弹出窗口打开我输入用户名和密码,然后弹出窗口必须关闭和用户名和密码传递给主要HTML中的Java脚本。 – Raj 2011-12-18 09:56:13

+0

是的。你有你的主页面,从你的主页面你弹出一个新的html页面来获取密码信息。您的新html页面包含对通过window.parent打开它的父页面的引用。在关闭弹出窗口之前,您需要在父页面中设置用户名和密码,然后在主父页面中继续。 – vassilo 2011-12-18 10:04:56

+0

@vassilo:window.parent是框架集中框架的父页面。与弹出窗口无关。 window.opener是打开弹出窗口的窗口。请参阅https://developer.mozilla.org/fr/DOM/window.parent和我的回答 – 2011-12-18 11:08:07

2

假设你的两个页面在同一个域上,你可以使用localStorage。

Popup.html:

var user = prompt('user name', ''); 
var password = prompt('password', ''); 
localStorage.user = user; 
localStorage.password = password; 

然后回到主页:

var user = localStorage.user; 
var password = localStorage.password; 

注1:我不会对安全性(其它评论比唐说,当然” t就像我做过的那样!)

注2:localStorage仅支持92%左右:http://caniuse.com/namevalue-storage

相关问题