Summarize the operation between the JavaScript (Iframe, window.open, window.showmodaldialog) parent window and child window

Source: Internet
Author: User


Turn from: http://www.cnblogs.com/haojianwei/archive/2010/03/02/1676707.html


One, the Iframe article

//&&&&&&&&&&&&&&&&&&&& Public method Start &&&&&&&&&&&&&&&

The parent object gets the value of the child window

Objectid is the window ID, ContentID is the element ID

function GetValue (Objectid,contentid)

{

var Isie = (Navigator.appname = = ' Microsoft Internet Explorer ')

if (Isie)

{//if it is IE

Alert (Document.frames (ObjectID). document.getElementById (ContentID). InnerHTML);

}

Else

{//If it is FF

Alert (document.getElementById (ObjectID). Contentdocument.getelementbyid (ContentID). InnerHTML);

InnerText is not supported under FF; Here's how to fix it

if (document.all) {

Alert (document.getElementById (' Div1 '). innertext);

} else{

Alert (document.getElementById (' Div1 '). textcontent);

//}

}

}

The parent object assigns a value to a child window

Objectid is the window ID, ContentID is the element ID

function SetValue (Objectid,contentid)

{

var Isie = (Navigator.appname = = ' Microsoft Internet Explorer ')

if (Isie)

{//if it is IE

Document.frames (ObjectID). document.getElementById (ContentID). innerhtml= "I am under IE through the parent window to assign value over";

}

Else

{//If it is FF

document.getElementById (ObjectID). Contentdocument.getelementbyid (ContentID). innerhtml= "I am under FF assignment through the parent window";

}

}

//&&&&&&&&&&&&&&&&&&&& Public method End &&&&&&&&&&&&&&&

1. Parent Window child window operation

Refresh:

document.getElementById ("Iframeid"). Src=document.getelementbyid ("Iframeid"). src+ "_=" +math.random ();

The above method sometimes needs to deal with the "src" attribute.

Take value:

The value of the parent window taking a child window

GetValue ("Iframe1", "Iframediv");

Assign value:

The parent window sets the value of the window element;

SetValue ("Iframe1", "Iframediv");

2. Child window Operation parent window

 

Refresh:

(1), window.parent.location.href=window.parent.location.href;

(2), window.parent.location.reload ();

(3), we can add

Take value:

Alert (Window.parent.document.getElementById ("Iframediv"). InnerHTML);

Assign value:

Window.parent.document.getElementById ("Iframediv"). Innerhtml= "I am the value from the IFrame of the child window";

off:

window.parent.opener=null;//If you do not add this sentence, you will be prompted to close the query window;

Window.parent.close ();

Second, window.open article

1. Parent Window child window operation

Open:

var win=null;

Win=window.open ("open.html", "Win", "width=200,height=200");

Maximize:

Window maximization

function Sonmaximize ()

{

if (win&&win.open&&!win.closed)

{

Win.moveto ( -4,-4);

Win.resizeto (SCREEN.AVAILWIDTH+8,SCREEN.AVAILHEIGHT+8);

}else{

Alert (' Not yet open window or already closed ');

}

}

Minimized:

Window minimized

function Sonminimize ()

{

if (win&&win.open&&!win.closed)

{

Win.resizeto (0,0);

Win.moveto (0,window.screen.width);

}else{

Alert (' Not yet open window or already closed ');

}

}

off:

Close window

function Closeson ()

{

if (win&&win.open&&!win.closed)

{

Win.opener=null;

Win.close ()

}else{

Alert (' Not yet open window or closed ');

}

}

Refresh:

Refresh

function Refreshson ()

{

if (win&&win.open&&!win.closed)

{

Win.location.reload ();

Win.focus ();

}else{

Alert (' window is not open or closed ');

}

}

To View the window size:

function Viewsonsize ()

{

if (win&&win.open&&!win.closed)

{

Alert (win.document.body.clientwidth+ ' * ' +win.document.body.clientheight);

Win.focus ();

}else

{

Alert (' Not yet open window or closed ');

}

}

Take value:

Alert (Window.document.getElementById ("Opendiv"). InnerHTML);

Assign value:

Win.document.getElementById ("Opendiv"). Innerhtml= "I am the value passed from the parent window";

2. Child window Operation Window

 

Refresh:

Window.opener.location.reload ();

The following method can also be

Window.parent.location.href=window.parent.location.href;

 

 

 

 

 

Close this window:

Close this window

function CloseWindow ()

{//window.opener.opener=null;

Window.close ();

}

To close the parent window:

Close parent window

function Closeparent ()

{//Firefox doesn't work if you want to work. Use the following method

Open Firefox, enter about:config in the address bar

Find the Dom.allow_scripts_to_close_windows and change to True

var Isie = (Navigator.appname = = ' Microsoft Internet Explorer ')

if (Isie) {//if it is IE

Window.opener.opener=null;

Window.opener.close ();

Window.close ();

}else{

Alert ("Firefox cannot be closed directly; you need the following settings 1.) Open Firefox, enter about:config;2 in the Address bar. Find the Dom.allow_scripts_to_close_windows and change to true");

}

}

Take value:

Alert (Window.opener.document.getElementById ("Opendiv"). InnerHTML);

Assign value:

Window.opener.document.getElementById ("Opendiv"). Innerhtml= "I am the value from the child window open;"

three, modal window chapter

1. Parent Window Action child window

parent Window JS Code:

var parvalue= "Now shows the value of the variable in the parent window";

var hao= "Jiang Jianwei";

function Showdailog (pagehref,title,height,width)

{

--------------Left Position

Screen.availheight declares the available width of the screen that displays the browser

var dleft = (screen.availheight-height)/2;

--------------Top Position

var dtop = (screen.availwidth-width)/2;

//---------------

Var Sret = window.showModalDialog (Pagehref,window,title, "Scrollbars=yes;resizable=no;help=no;status=no;center=yes ;d ialogtop=25;dialogleft= "+ dleft +";d ialogtop= "+ dtop +";d ialogheight= "+height+" px;dialogwidth= "+Width+" PX; ");

--------return

if (Sret = = "Refresh")//This is to use the return value to refresh the parent page

{

Window. Test= "true";

Window.location.reload ();

Alert (window. Test);

}

}

function test ()

{

Alert ("Method for a modal window to successfully invoke the parent window");

}

2. Modal window operation parent window

var parentwin=window.dialogarguments;

Refresh:

ParentWin.location.reload ();

Take value:

Alert (ParentWin.document.getElementById ("Showmodaldialogdiv"). InnerHTML)//Get the object in the parent window

Alert ("I am a variable from the parent window >>>" +parentwin.parvalue); To get a variable in the parent window

Call the parent window JS method:

Parentwin.test (); Calling methods in the parent window

Assign value:

ParentWin.document.getElementById ("Showmodaldialogdiv"). Innerhtml= "I am the value from the child window ShowModalDialog";

 

Close this window:

Close this window

function CloseWindow ()

{

Window.parent.close ();

}

To close the parent window:

Close parent window

function Closemodal ()

{

var Isie = (Navigator.appname = = ' Microsoft Internet Explorer ')

if (Isie) {//if it is IE

Window.parent.parent.close ();

Parentwin.opener=null: If you change the above to this line, you cannot close the parent window,

Parentwin.close ();

Window.parent.parent.parent.parent.close (); This only closes the modal window itself and is currently only tested under IE6

}else{

Alert ("Firefox cannot be closed directly; you need the following settings 1.) Open Firefox, enter about:config;2 in the Address bar. Find the Dom.allow_scripts_to_close_windows and change to true");

}

JavaScript parent Windows interoperate with child windows. rar

Related Article

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.