javascript呼叫父視窗(父頁面)的方法
阿新 • • 發佈:2019-01-30
Html程式碼
- <html>
- <head>
- <title>主頁面</title>
- <scripttype="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>
- <formname="form1"id="form1">
- <inputtype="text"name="username"id="username"/>
- <inputtype
- <inputtype="button"value="測試呼叫彈出視窗中的全域性變數"onclick = "parentInvokeChild()">
- </form>
- </body>
- </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>