JavaScript calls to other pages of the JS function or variable script _javascript tips

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.