window.parentä¸window.openerçåºå« javascriptè°ç¨ä¸»çªå£æ¹æ³
1ï¼ window.parent
æ¯iframe页é¢è°ç¨ç¶é¡µé¢å¯¹è±¡
举ä¾:
a.html
Html代ç <html>
<head><title>ç¶é¡µé¢</title></head>
<body>
<form name="form1" id="form1">
<input
type="text" name="username" id="username"/>
</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>
å¦ææ们éè¦å¨b.htmä¸è¦å¯¹a.htmä¸çusernameææ¬æ¡èµå¼ï¼å°±å¦å¾å¤ä¸ä¼ åè½ï¼ä¸ä¼ åè½é¡µå¨Ifrmaeä¸ï¼ä¸ä¼ æååæä¸ä¼ åçè·¯å¾æ¾å
¥ç¶é¡µé¢çææ¬æ¡ä¸
æ们åºè¯¥å¨b.htmlä¸å
Html代ç
<script type="text/javascript">
var _parentWin =
window.parent ;
_parentWin.form1.username.value = "xxxx" ;
</script>
å®ä¾å°åï¼ å®ä¾/a.html
æºç ï¼
1.a.html
Html代ç
<html>
<head>
<title>主页é¢</title>
<script>
/** 为æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åéèæ·»å çæµè¯åé */
var parentVairous = "为æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åéèæ·»å çæµè¯åé";
function parentInvokeIFrame()
{
var
iframeTest = document.frames["iframeTest"];
//使ç¨document.getElementById("iframeTest");åæ ·å¯ä»¥
alert(iframeTest.document.body.innerHTML);
alert(iframeTest.iFrameVair);
}
</script>
</head>
<body>
<form name="form1"
id="form1">
<input type="text" name="username"
id="username"/>
<input type = "button" value =
"ç¶çªå£è°ç¨IFrameåçªå£ä¸çå
容" onclick = 'parentInvokeIFrame()'/>
</form>
<iframe src="b.html" width = '100%' id =
'iframeTest'></iframe>
</body>
</html>
1.b.html
Html代ç
<html>
<head>
<title></title>
<script
type="text/javascript">
/**
为æµè¯ç¶çªä½è°ç¨IFrameåçªä½çå
¨å±å½æ°èæ·»å çåçªå£å
¨å±å½æ° */
var iFrameVair =
"æµè¯ç¶çªä½è°ç¨IFrameåçªä½çå
¨å±å½æ°";
function
UpdateParent()
{
var _parentWin =
window.parent ;
_parentWin.form1.username.value = "xxxx"
;
}
function
childInvokeParent()
{
var parentVairous =
window.parent.window.parentVairous;
alert(parentVairous);
}
</script>
</head>
<body>
<form name="form1"
id="form1">
<p> </p>
<p
align="center">
<input type = "button"
name = "button"
id =
"button"
value = "æ´æ°ä¸»é¡µé¢çUserNameå
容"
onclick = "UpdateParent()">
<input type = "button"
name = "button2"
id = "button2"
value
= "æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åé"
onclick =
"childInvokeParent();"/>
</p>
<p>
</p>
</form>
</body>
</html>
ps:ä¸è½è·¨åè·åï¼ä¾å¦iframeçsrcæ¯'
http://www.xxx.ccc/'å°±ä¸å¯ä»¥
2ï¼ window.opener
æ¯window.open æå¼çå页é¢è°ç¨ç¶é¡µé¢å¯¹è±¡
å®ä¾å°åï¼ å®ä¾/a.html
æºç ï¼
2.a.html
Html代ç
<html>
<head>
<title>主页é¢</title>
<script type="text/javascript">
/**
为æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åéèæ·»å çæµè¯åé */
var parentVairous =
"为æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åéèæ·»å çæµè¯åé";
/**
*
å 为ä¸åäºIFrameï¼IFrameæid,window.open()ä¸IFrameçç¶åçªå£ç模å¼ä¸åï¼,
*
æ以å½æ¯éè¿window.open()æ¹æ³æå¼ä¸ä¸ªæ°çªå£ä½¿, å¿
é¡»æä¸ä¸ªæ°çªå£ç对象
* å½ç¶å¿
é¡»å
让åçªå£å¼¹åºæ¥,
æè½è°ç¨åçªå£ä¸çåé, å¦åæåºå¼å¸¸
*/
var OpenWindow;
function openSubWin()
{
OpenWindow =
window.open('b.html', 'newwindow', 'height=1024, width=1300, top=0, left=0,
toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no,
status=no');
}
function parentInvokeChild()
{
if(OpenWindow)//å½ç¶å¿
é¡»å
让åçªå£å¼¹åºæ¥, æè½è°ç¨åçªå£ä¸çåé,
å¦åæåºå¼å¸¸
{
alert(OpenWindow.iFrameVair);
}
}
</script>
</head>
<body>
<form
name="form1" id="form1">
<input type="text" name="username"
id="username"/>
<input type="button" value="å¼¹åºå页é¢" onclick =
"openSubWin()">
<input type="button" value="æµè¯è°ç¨å¼¹åºçªå£ä¸çå
¨å±åé"
onclick = "parentInvokeChild()">
</form>
</body>
</html>
2.b.html
Html代ç
<html>
<head>
<title>å页é¢</title>
<script
type="text/javascript">
/** 为æµè¯ç¶çªä½è°ç¨IFrameåçªä½çå
¨å±å½æ°èæ·»å çåçªå£å
¨å±å½æ°
*/
var iFrameVair = "æµè¯ç¶çªä½è°ç¨IFrameåçªä½çå
¨å±å½æ°";
function UpdateParent()
{
var _parentWin =
window.opener;
_parentWin.form1.username.value = "xxxx"
;
}
function childInvokeParent()
{
var parentVairous =
window.opener.window.parentVairous;
alert(parentVairous);
}
</script>
</head>
<body>
<form name="form1"
id="form1">
<p> </p>
<p align="center">
<input type="button"
onclick =
"UpdateParent();"
name="button"
id="button"
value="æ´æ°ä¸»é¡µé¢çUserNameå
容">
<input type = "button"
name = "button2"
id = "button2"
value =
"æµè¯IFrameåçªå£è°ç¨ç¶çªå£çå
¨å±åé"
onclick =
"childInvokeParent();"/>
</p>
<p> </p>
</form>
</body>
ç»è¿hanjsçæéï¼ç¡®å®éè¦æ³¨æçæ¯ï¼æ¨¡æçªå£çåçªå£æ¯æ²¡æåæ³ä¿®æ¹ç¶çªå£é¡µé¢ä¸çä»»ä½å
容çã
ä¾å¦ä¿®æ¹ï¼OpenWindow =
window.open('b.html', 'newwindow', 'height=1024, width=1300, top=0, left=0,
toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no,
status=no');
为ï¼OpenWindow =
window.showModalDialog("b.html",'newwindow',"dialogHeight:100px,center:yes,resizable:no,status:no");
å¨åçªå£ä¸å½å¸æä¿®æ¹ç¶çªå£ä¸çå
容æ¶ï¼ä¼å¼¹åºâææâ为空æä¸æ¯å¯¹è±¡çé误ï¼èè¿éçâææâå°±æ¯ä½ æ³ä¿®æ¹çç¶çªå£ä¸çå
容