在新窗口中访问 JavaScript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16215170/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:45:15  来源:igfitidea点击:

access JavaScript variable in new window

javascriptvariables

提问by kdormuth

This is my first JavaScript attempt, so I apologize if things are a little mangled.

这是我第一次尝试 JavaScript,所以如果事情有点混乱,我深表歉意。

I have two html pages (Certificate1.html and Certificate2.html). What I'm trying to do is prompt the user for his/her name on Certificate1.html, then pass that information to Certificate2.html. At this point the user's name will be displayed in a certificate that (s)he can print.

我有两个 html 页面(Certificate1.html 和 Certificate2.html)。我想要做的是在 Certificate1.html 上提示用户输入他/她的名字,然后将该信息传递给 Certificate2.html。此时,用户的姓名将显示在他可以打印的证书中。

Both html pages reference the same JavaScript file (Certificate1.js). The first page calls passName():

两个 html 页面都引用相同的 JavaScript 文件 (Certificate1.js)。第一个页面调用passName():

function passName() {
    FirstN = document.frmUserName.inFirstN.value;
    LastN = document.frmUserName.inLastN.value;
   // alert(FirstN); // good
   // alert(LastN); // good
    var Cert = window.open("Certificate2.html");
    Cert.FirstN = FirstN;
    Cert.LastN = LastN;
    //alert(Cert.FirstN); //good
    //alert(Cert.LastN); //good
}

This seems to be working correctly. Where I'm stuck is the function placeName() that Certificate2.html calls. I have it firing onLoad, and I know it's accessing the function correctly (I just stuck an alert in there and it came up). I don't know how to access the FirstN and LastN variables that I passed to Cert in passName(). I've tried document.FirstN but I get "undefined." How can I access the FirstN and LastN variables that I (think I) passed?

这似乎工作正常。我被卡住的地方是 Certificate2.html 调用的函数 placeName()。我让它在加载时触发,并且我知道它正在正确访问该功能(我只是在那里卡了一个警报然后它出现了)。我不知道如何访问我在 passName() 中传递给 Cert 的 FirstN 和 LastN 变量。我试过 document.FirstN 但我得到“未定义”。如何访问我(认为我)传递的 FirstN 和 LastN 变量?

Thanks! -Kristin

谢谢!-克里斯汀

UPDATE:

更新:

Got it!!!

知道了!!!

I didn't need to access it via window.opener - I had passed in the variables to the window, so I was able to access them directly.

我不需要通过 window.opener 访问它——我已经将变量传递给了窗口,所以我能够直接访问它们。

function placeName() {
    //alert(FirstN);
    document.getElementById("pUserName").innerHTML = FirstN + " " + LastN;
}

Thanks guys!! -Kristin

多谢你们!!-克里斯汀

回答by Oliboy50

HTML LocalStorage (HTML5) http://diveintohtml5.info/storage.html

HTML LocalStorage (HTML5) http://diveintohtml5.info/storage.html

in your first file :

在你的第一个文件中:

localStorage.setItem("FirstN", document.frmUserName.inFirstN.value);

in the second one :

在第二个:

var FirstN = localStorage.getItem("FirstN");

or simply set Cookies...

或者简单地设置 Cookies...

http://www.w3schools.com/js/js_cookies.asp

http://www.w3schools.com/js/js_cookies.asp

But i think this should be done using PHP or at least not JS

但我认为这应该使用 PHP 或至少不是 JS 来完成

回答by StuckAtWork

Use the window.openerobject in the new window.

window.opener在新窗口中使用对象。

window.opener.FirstN

W3Schools Examples

W3Schools 示例

Can I pass a JavaScript variable to another browser window?

我可以将 JavaScript 变量传递给另一个浏览器窗口吗?

The following works with my setup:

以下适用于我的设置:

first.html

第一个.html

<html>
 <script>
   var Var1 = "MyStringVar";
   var Var2 = 123;
   var win = window.open("second.html");
 </script>
</html>

second.html

第二个.html

<html>
 <script>
   alert(window.opener.Var1);
   alert(window.opener.Var2);
 </script>
</html>

回答by kdormuth

UPDATE:

更新:

Got it!!!

知道了!!!

I didn't need to access it via window.opener - I had passed in the variables to the window, so I was able to access them directly.

我不需要通过 window.opener 访问它——我已经将变量传递给了窗口,所以我能够直接访问它们。

function placeName() {
    //alert(FirstN);
    document.getElementById("pUserName").innerHTML = FirstN + " " + LastN;
}

Thanks guys!! -Kristin

多谢你们!!-克里斯汀

回答by Nandeesh

In first window file:

在第一个窗口文件中:

window.yourVariable = value;

window.yourVariable = 值;

In second window file:

在第二个窗口文件中:

window.opener.yourVariable

window.opener.yourVariable

decription : declare yourVariable as global and bind it to parent window. If new window is opened from the parent window, then the data can be accessed from 'window.opener'. Since yourVariable is declared as global, the values of parent window can be accessed from the child window by using 'window.opener.yourVariable'

说明:将 yourVariable 声明为全局变量并将其绑定到父窗口。如果从父窗口打开新窗口,则可以从“window.opener”访问数据。由于 yourVariable 被声明为全局变量,因此可以使用“window.opener.yourVariable”从子窗口访问父窗口的值