简单的说,就是一个在弹出窗口之后可以做其它的事,即window.open

另一个在弹出窗口之后不能做其它的事,只能是关闭了当前的窗口之后才能做其它的事,即window.showModalDialog

那么两者在使用上有什么不同呢?他们分别是如何和父窗口进行交互的呢?

先来看window.showModalDialog的例子:

我这里现在有一个父窗体parent.jsp,它里面有一个方法

function openChild(){
                var temp = window.showModalDialog("child.jsp",window,'dialogWidth=400px;dialogHeight=200px');
                document.getElementById("fromChildName").value = temp.childName;
                document.getElementById("fromChildAge").value = temp.childAge;
    }

这里面,我们第二个参数传递为window,也就是把当前页面做为参数传递到子窗口中,temp 为子窗口的返回值

再来看子窗口child.jsp页面:

function fromParent(){
               var parName = window.dialogArguments.document.getElementById("parName").value;  //得到父窗口中的姓名
               var parAge = window.dialogArguments.document.getElementById("parAge").value
               document.getElementById("fromParName").value =  parName;
              document.getElementById("fromParAge").value = parAge;
    }

从上面我们就可以看出,在父窗口中我们传递了window这个参数,然后在子窗口中,我们用window.dialogArguments直接到取了父窗口中id = "parName"的属性值

再来说这个返回值temp是怎么回事?

function toParent(){
               var obj = new Object();
               obj.childName = document.getElementById("childName").value;
               obj.childAge = document.getElementById("childAge").value;
               window.returnValue = obj;
               window.close();
   }

我们用window.returnValue的方式直接把一个对象返回到父窗口,然后父窗口根据对象中的属性直接取出其中的值就OK了

那么,我们可不可以直接调用父窗口中的方法呢?

答案是肯定的:

function fromParentFunction(){
               window.dialogArguments.parFunction();
      }

我们用window.dialogArguments + 父窗口的方法名,就直接可以调用父窗口的方法

上面是window.showModalDialog如何来进行子父窗口间的传递值,那么,接下来看下window.open是如何进行子父窗口间的传值:

在父窗口parent.jsp页面中:

function openChild(){
               var obj = window;
               obj.name = "张三";
               obj.age = "18";
               window.open('child.jsp','我是弹出子窗口','height=200,width=400,top=200,left=400,toolbar=no,menubar=no,

scrollbars=no, resizable=no,location=no, status=no');
        }

我们定义变量obj = window,再通过属性赋值把对象传递过去,接下来看下子窗口:

function fromParent(){
              alert("得到父窗口的中姓名值:"+ this.opener.name);
              alert("得到父窗口中的年龄值:"+ this.opener.age);
      }

利用this.opener.属性名  就可以得到父窗口中的变量值

那么如何把值子窗口中的值再返回到父窗口中呢?

function toParent(){
                //把子窗口中的值传递给父窗口,document.getElementById("name").value得到子窗口的值
             this.opener.document.getElementById("parName").value = document.getElementById("childName").value;
             this.opener.document.getElementById("parrAge").value = document.getElementById("childAge").value;
            window.close();
     }

这里的parName是父窗口中的id = 'parName' ,也就是说,可以在子窗口中利用 this.opener. + 父窗口元素 赋值给父窗口

window.showModalDialog 与window.open传递参数的不同?的更多相关文章

  1. window.showModalDialog与window.open()使用

    window.showModalDialog 有些浏览器不兼容,尝试用window.open() 封装替代,需要打开子窗口后向父窗口传递数据. <html> <script src= ...

  2. window.open、window.showModalDialog和window.showModelessDialog 的区别[转]

    一.前言 要打开一个可以载入页面的子窗口有三种方法,分别是window.open.window.showModalDialog和window.showModelessDialog. open方法就是打 ...

  3. window.showModalDialog以及window.open用法简介

    .可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象.例如:------------------------------parent.htm<script& ...

  4. window.showModalDialog的基本用法

    window.showModalDialog的基本用法 showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.show ...

  5. window.showModalDialog

    //新版本谷歌没有window.showModalDialog,创建一个window.openif(window.showModalDialog == undefined){  window.show ...

  6. window.showModalDialog()之返回值

    window.showModalDialog的基本用法 showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.show ...

  7. window.open || window.showModalDialog || window.showModelessDialog

    http://dwcmayday201204063551.iteye.com/blog/1621751 http://www.cnblogs.com/zhangyi85/archive/2009/09 ...

  8. javascript window.showModalDialog不兼容goole解决方案

    window.showModalDialog不兼容goole解决方案 一.弹框方案: 1.window.open; 2.window.showModalDialog; 3.div制作窗口:(本节忽略) ...

  9. window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法

    项目中使用案例: 父窗体 <s:form namespace="/forexagent" id="listSearchForm" name="t ...

随机推荐

  1. 对人脑处理视觉的描述(摘《学习OpenCV(中文版)》)

    人脑将视觉信号划分入很多个通道,将各种不同的信息输入你的大脑.你的大脑有一个关注系统,会根据任务识别出图像的重要部分,并做重点分析,而其他部分则分析得较少 .在人类视觉流中存在大量的反馈,但是目前我们 ...

  2. ansibleplaybook的使用

    1.简单格式要求 [root@ansibleserver ansible]# cat nagios.yml --- - hosts: nagiosserver tasks: - name: ensur ...

  3. string subscript out of range

    刚刚练习华为机试上的题目遇到了这个问题,奉上两个小题: //题目描述 // //描述: //输入一个整数,将这个整数以字符串的形式逆序输出 //程序不考虑负数的情况,若数字含有0,则逆序形式也含有0, ...

  4. 150个JS特效脚本

    收集了其它一些不太方便归类的JS特效,共150个,供君查阅. 1. simplyScroll simplyScroll这个jQuery插件能够让任意一组元素产生滚动动画效果,可以是自动.手动滚动,水平 ...

  5. ps做gif 登陆下拉菜单效果

    作者这里仅介绍登录动画的制作思路和简单过程.一些细节的制作,如登录框,每一帧的图像等都需要自己根据参考图慢慢完成.最终效果 1.新建大小适当的文件,背景填充暗蓝色.首先设计一个底座,主要用图层样式来完 ...

  6. BITED-Windows8应用开发学习札记之一:Win8应用开发入门

    基于WinRT的Metro应用是我对Windows8的第一印象,简洁方块配以简单色调真的有沁人心脾的美感. 非常幸运,BITED在这个暑假的这个小学期里有幸学习到一门课程:软件工程基础实践,老师邀请了 ...

  7. JavaIO之RandomAccessFile随机访问文件

    package test.java.io; import java.io.RandomAccessFile; public class RandomAccFile { public static vo ...

  8. Windows Azure Platform 系列文章目录

    Windows Azure Platform (一) 云计算的出现 Windows Azure Platform (二) 云计算的分类和服务层次 Windows Azure Platform (三) ...

  9. 【Excel】Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}:

    [Excel]Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-0000000000 ...

  10. rdlc Report Viewer Configuration Error

    主要是web.config没有配置 方法参考http://msdn.microsoft.com/zh-cn/library/ms251661.aspx