最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

access JavaScript variable in new window - Stack Overflow

programmeradmin3浏览0评论

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

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.

Both html pages reference the same JavaScript file (Certificate1.js). The first page calls 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?

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.

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

Thanks guys!! -Kristin

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

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.

Both html pages reference the same JavaScript file (Certificate1.js). The first page calls 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?

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.

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

Thanks guys!! -Kristin

Share Improve this question edited Apr 25, 2013 at 13:30 kdormuth asked Apr 25, 2013 at 12:47 kdormuthkdormuth 551 gold badge1 silver badge9 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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...

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

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

Use the window.opener object in the new window.

window.opener.FirstN

W3Schools Examples

Can I pass a JavaScript variable to another browser window?

The following works with my setup:

first.html

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

second.html

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

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.

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

Thanks guys!! -Kristin

In first window file:

window.yourVariable = value;

In second window file:

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'

发布评论

评论列表(0)

  1. 暂无评论