posts - 495,comments - 227,trackbacks - 0

1 、不能使用 window.parent

Window.parent 是用来在 frame 中进行操作的,在对话框中不能用来操作父窗口对象

 

2 、正确的做法

 

调用 modaldialog 时通过传参数的方式操作

例:

需求

父窗口页面为 a.html 子窗口页面为 b.html a.html 中有文本框 id test1 ,在打开的对话框中点击按钮,将 a.html 的文本框值改为“子窗口值”。

实现

打开对话框时把 test1 作为参数传给子窗口,在子窗口中获取参数,将参数对象(即 a.html 中传过来的 text 对象)的 value 属性值设置为“子窗口值”

注意:这里只能传 id ,不能传 name

 

a.html 代码如下

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>a.html</title>

</head>

<body>

<input type=text id=test1 value=''>

<input type=button value=" OK " onclick='window.showModalDialog("b.html", test1)'>

</body>

</html>

 

b.html 代码如下

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>b.html</title>

<script language=javascript>

function func1(){

//获取父窗口传过来的参数

var ptextid = window.dialogArguments;

if(ptextid != undefined){

//将父窗口传过来的对象的值改为“子窗口值”

ptextid.value = "子窗口值";

//关闭子窗口

window.close();

}

}

</script>

</head>

<body>

<input type=button value=" OK " onclick=func1()>

</body>

</html>

 

 

如果需要操作的父窗口对象比较多,也可以将 window 或window.document作为参数传给子窗口。

例:

需求

a.html 中添加 id 为“ aform ”的的 form form 中有 id test2 的文本框,在 b.html 中,除了进行上面的操作之外,还要将 test2 的值改为“子窗口值 2 ”,并将 form 提交到 c.html

实现 1

a.html 中打开对话框的函数改为如下方式 :

window.showModalDialog("b.html", window.document) ;

 

b.html func1() 改为如下 :

 

function func1(){

var pdoc = window.dialogArguments;

if(pdoc!=undefined){

pdoc.all.test1.value="子窗口值";

pdoc.all.test2.value="子窗口值2";

pdoc.all.aform.action="c.html";

pdoc.all.aform.submit();

}

}

 

 

实现 2

因为在子窗口中对父窗口进行的操作比较多,也可以采用execScript的方式实现。

 

a.html 中打开对话框的函数改为如下方式 :

window.showModalDialog("b.html", window) ;

 

添加 javascript 函数如下

function func(){

test1.value="子窗口值";

document.all.test2.value="子窗口值2";

aform.action="c.html";

aform.submit();

}

 

b.html func1() 改为如下 :

 

function func1(){

var pwin = window.dialogArguments;

if(pwin!=undefined){

var codeStr = "func();"

pwin.execScript(codeStr,"javascript");

window.close();

}

}

posted on 2006-09-07 12:41 SIMONE 阅读(359) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: