window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口

项目中经常遇到一个业务逻辑:在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口(或局部更新A窗口)(或将数据传回A窗口)

以下是从实际项目中截取出来和window.open()方法相关的代码,业务逻辑如下:

  1. 点击父窗口的div标签(id="addMatchSchedule"),出发点击事件,打开子窗口;

  2. 点击子窗口的button按钮,触发点击时间,即调用addSchduleItem()函数;

  3. addSchduleItem()函数执行 window.opener.showAddMatchSchedule(idList,iconList,matchProductNameList)方法;即回调父窗口的showAddMatchSchedule()函数,在父窗口的div(id="matchFrame")中展示子窗口回调过来的数据;

以上三步实现了两个目的:a.由子窗口向父窗口传递数据,b.在父窗口即时更新的接受的数据;

一句话概括思路:在父窗口中打开子窗口,在子窗口中调用父窗口的方法

核心方法:window.open()  (方法介绍在本文尾部)

核心概念:window.opener (方法介绍在本文尾部)

父窗口标签:

<div style="width:140px; height:60px; position:relative;dislay:inline-block; margin-right:20px;display:inline-block;cursor: pointer;" id="addMatchSchedule"> </div>
<div id="matchFrame" style="height:70px;display:inline-block;"></div>

父窗口js代码:

$("#addMatchSchedule").click(function(){
window.open('<%=basePath%>product/goAddMatchSchdule.do?',"新增","width=500,height=480,screenX=400,screenY=100")
})

  

父窗口js代码:

//可忽略该函数的具体内容   
function showAddMatchSchedule(idList,iconList,matchProductNameList){
var matchFrame =$("#matchFrame");
var len = idList.length;
for(var i=0;i<len; i++){
var id = idList[i];
var src = iconList[i];
var matchProductName = matchProductNameList[i];
var oDiv = $("<div class='oDiv'></div>");
var inputId=$("<input type='hidden' name='productMatchId' value='"+id+"'></input>");
var imgIcon=$("<img class='img21' src = '<%=basePath%>"+src+"'></img>");
var span=$("<span style='position:absolute;top:60px;left:10px;'>"+matchProductName+"</span>"); <%-- var imgIcon=$("<img class='img21' style='margin-right:20px;' src = '<%=basePath%>"+src+"'></img>"); --%>
inputId.appendTo(oDiv);
imgIcon.appendTo(oDiv);
span.appendTo(oDiv);
oDiv.appendTo(matchFrame);
}
}

  子窗口标签:

<a class="btn btn-small btn-info" onclick="addSchduleItem();" title="确定" >确定</a>

  子窗口代码:

//添加搭配,并将数据传回编辑页面;可忽略本函数的具体业务代码
function addSchduleItem(){
var idList = new Array();
var iconList = new Array();
var matchProductNameList = new Array();
$("input:checked").each(function(){
var id = $(this).val();
idList.push(id);
var src = $(this).parent().next().val();
iconList.push(src);
var matchProductName = $(this).parent().next().next().val();
matchProductNameList.push(matchProductName);
})
if(idList.length == 0){
alert("请选择搭配方案")
return;
}
  
if (window.opener != null && !window.opener.closed) {
window.opener.showAddMatchSchedule(idList,iconList,matchProductNameList);
}
}

 window.open()简介(以具体情况为例):

  window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')   //该句写成一行代码

参数解释:
       window.open   弹出新窗口的命令; 
  'page.html'   弹出窗口的文件名; 
  'newwindow'   弹出窗口的名字(不是文件名),非必须,可用空''代替; 
  height=100   窗口高度; 
  width=400   窗口宽度; 
  top=0   窗口距离屏幕上方的象素值; 
  left=0   窗口距离屏幕左侧的象素值; 
  toolbar=no  是否显示工具栏,yes为显示; 
  menubar,scrollbars   表示菜单栏和滚动栏。 
  resizable=no   是否允许改变窗口大小,yes为允许; 
  location=no   是否显示地址栏,yes为允许; 
  status=no   是否显示状态栏内的信息(通常是文件已经打开),yes为允许;

window.opener 简介

window.opener 实际上就是通过window.open打开的子窗体的父窗体

本文中window.opener.showAddMatchSchedule(idList,iconList,matchProductNameList);表示直接调用父窗口的showAddMatchSchedule()方法

项目总结03:window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口的更多相关文章

  1. VUE 父组件与子组件交互

    1. 概述 1.1 说明 在项目过程中,会有很多重复功能在多个页面中处理,此时则需要把这些重复的功能进行单独拎出,编写公用组件(控件)进行引用.在VUE中,组件是可复用的VUE实例,此时组件中的dat ...

  2. iframe通信相关:父操作子,子操作父,兄弟通信

    这里写window和document是认为代表了BOM和DOM(个人理解不一定对) 拿到了window就能操作全局变量和函数 拿到了document就能获取dom,操作节点 父操作子 window:选 ...

  3. vue:再vue-cli项目中使用window以及调用window上的方法

    一: 1:在main.js中 Vue.prototype.myfunction = function() {/*你的自定义Vue方法*/} 2:在mounted(或其他生命周期中) 或者 method ...

  4. VB的一些项目中常用的通用方法-一般用于验证类

    1.VB的一些项目中常用的通用方法: ' 设置校验键盘输入值,数字 Public Function kyd(key As Integer) As Integer Dim mychar mychar = ...

  5. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

  6. window.opener方法的使用 js跨域

    原文:window.opener方法的使用 js跨域 最近公司网站登陆加入了第三方登陆.可以用QQ直接登陆到我们网站,在login页面A中点QQ登陆时,调用了一个window.open文件打开一个lo ...

  7. 使用iframe父页面调用子页面和子页面调用父页面的元素与方法

    在实际的项目开发中,iframe框架经常使用,主要用于引入其他的页面.下面主要介绍一下使用iframe引入其他页面后,父页面如何调用子页面的方法和元素以及子页面如何调用父页面的方法和元素. 1.父页面 ...

  8. vue 父组件调用子组件内置方法

    背景介绍:外派到泰康做项目.这个项目中有个选择组织的功能,是一个树桩结构的懒加载,于是我就element-ui的tree组件封装了一个公共的组件. 但是后来发现他们的公司组织结构不是都请求的同一个接口 ...

  9. window.opener方法的使用 js 跨域

    用到了这个方法: window.opener.location.reload() 与 window.opener.location.href=window.opener.location.href 都 ...

随机推荐

  1. 对比深度学习十大框架:TensorFlow 并非最好?

    http://www.oschina.net/news/80593/deep-learning-frameworks-a-review-before-finishing-2016 TensorFlow ...

  2. Matplotlib中文乱码问题

    一.找到并修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,用记事本打开matplotlibrc文件:找到f ...

  3. mac+windows下从git上拉取项目及运行

    一.Mac下从git拉取项目 1. 拉项目 打开终端,先进入想放置项目的目录.假设进入workfile目录,输入cd workfile. 进入workfile目录后:输入git clone 链接(gi ...

  4. t959 unknown device 解决办法

    换机器没用 换数据线没用 最后装了Kies3,好了! -------- 更新 跟数据线也有关系 换一条三星自带的试试

  5. 修改Http消息的消息头Host

    在 HttpURLConnection 类中直接使用如下代码无法修改Host的值: connection.setRequestProperty("Host", host); 需要在 ...

  6. C# 反射获取所有视图

    原地址:忘了 controller 的 action 加上属性 [System.ComponentModel.Description("菜单列表")]  且  返回值为 Syste ...

  7. tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')

    tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT') tf.pad 是扩展的意思,其中[0, 0], [1, 0] 分别代表的是[上, ...

  8. shell 脚本传参

    在 shell 中我们会见到  $0.$1.$2这样的符号,这是什么意思呢? 简单来说 $0 就是你写的shell脚本本身的名字,$1 是你给你写的shell脚本传的第一个参数,$2 是你给你写的sh ...

  9. jenkins 添加节点问题

    没有 Launch agent via Java Web Start 选项 Manage Jenkins > Configure Global Security > TCP port fo ...

  10. kubectl windows

    https://storage.googleapis.com/kubernetes-release/release/v1.10.3/bin/windows/amd64/kubectl.exe