The method of calling a function, a variable, is to first obtain the window object that you want to call the function, and then call it by Window.method () or window.variable.
The following example shows how a pop-up window invokes methods and variables in the parent window.
Parent window: 1.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> Cloud Habitat Community www.jb51.net other page JS variable calling method code </title> <script type=" Text/javascript "& Gt var thehellostr = "Hello all." function SayHello () {alert ("Hello, Method"); function openbtnclicked () {var newwin=window.open (' 2.html ', ' Test ', ' toolbar=no,location=no,top=100,left=10 0,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width=300,height=200 "); Newwin.focus (); function SayHello2 () {Window.sayhello (); } </script> </pead> <body> <input type= "button" Name= "openbtn" value= "open Window" onclick= "ope nbtnclicked () "/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Pop-up window: 2.html
<textarea id="runcode30330"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> Cloud Habitat Community www.jb51.net other page JS variable calling method code </title> <script type=" Text/javascript "& Gt function btn1clicked () {Window.opener.sayHello (); function btn2clicked () {var hellomsg = window.opener.theHelloStr; alert (hellomsg); } </script> </pead> <body> <input type= "button" Name= "BTN1" value= "The method of calling the parent window" Onclick= "BT n1clicked () "/> <input type=" button "Name=" BTN2 "value=" calls the parent window's variable "onclick=" btn2clicked () "/> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Description: In 2.html, Window.opener is used to get the window object of the parent window of the pop-up.
Reverse thinking: In a page, you can also use the method or variable by Window.method () or window.variable, such as function SayHello2 () {Window.sayhello (), in 1.html (); Just because window is the default object, you don't need to add it at all.