Chrome37+代替window.showModalDialog模态窗口方法
来源:互联网 发布:印度不服中国 知乎 编辑:程序博客网 时间:2024/06/05 20:18
原理是利用html5中的window.open()和window.postMessage()
window.postMessage详情请查看:https://developer.mozilla.org/zh-CN/docs/Web/API/Window.postMessage
首先是自己写的dialog.js
function Dialog() { var d = this.dialog = Dialog.prototype; d.Popup = null; //初始化 d.init = function () { //執行頁面消息監聽事件 if (!d.isModalDialog()) { d.addEventListener(); } }; /** * 後台系統彈窗 * @param {String} url 窗口地址 * @param {Int} width 窗口寬度 * @param {Int} height 窗口高度 * @param {Boolean} bScroll 窗口是否允許滾動條 * @param {Function} evt[可選] 窗口返回數據回調函數 */ d.show = function (url, width, height, bScroll, evt) { d.DialogCompleteEventName = evt; var scroll = (bScroll == false) ? "no" : "yes"; var left = (window.screen.width - width) / 2; var top = (window.screen.height - height) / 2; if (d.isModalDialog()) { var returnValue = window.showModalDialog(url, '', 'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;dialogLeft:' + left + 'px;dialogTop:' + top + 'px;status:yes;edge:Sunken;scroll:' + scroll + ';help:no;resizable:no;'); if (returnValue == null) { returnValue = window.returnValue; } window.returnValue = null; d.callback(returnValue); } else { if (d.Popup != null) { d.Popup.close(); } d.Popup = window.open(url, '', 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',directories=no,toolbar=no,menubar=no,copyhistory=no,scrollbars=' + scroll + ',resizable=no,location=no,status=yes'); if (d.Popup != null) { d.Popup.focus(); //发送消息 setTimeout(function () { d.Popup.postMessage("", "*"); }, 600); } } }; //子頁面處理完成后向父級頁面提交返回值 d.sendMessage = function (returnValue) { if (!d.isModalDialog() && window.opener != null) { window.opener.postMessage(returnValue, "*"); } else { window.returnValue = returnValue; if (window.opener != null) { window.opener.returnValue = returnValue; } } window.close(); }; //執行回調函數 d.callback = function (returnValue) { if (typeof d.DialogCompleteEventName == "function") { d.DialogCompleteEventName(returnValue); } }; //是否存在模态窗口 d.isModalDialog = function () { if (typeof window.showModalDialog == "undefined") { return false; } else { return true; } }; //定義頁面消息監聽事件 d.addEventListener = function () { function onMessage() { d.callback(event.data); } if (typeof window.addEventListener != 'undefined') { window.addEventListener('message', onMessage, false); } else if (typeof window.attachEvent != 'undefined') { window.attachEvent('onmessage', onMessage); } }; d.init();}var oDialog = new Dialog();
然后调用如下:
1、调用页面(父级页面),引用dialog.js,调用函数弹窗:oDialog.show(url, width, height, bScroll, evt);
url: 窗口地址,必填
width: 窗口寬度,必填
height: 窗口高度,必填
bScroll: 窗口返回數據回調函數,必填
evt: 窗口返回數據回調函數,可选
2、彈窗頁面(子级页面),引用dialog.js,调用函数返回数据:oDialog.sendMessage(returnValue);
returnValue: 彈窗要返回的數據
示例:
index.aspx調用頁面
function selectItems() { var url = "../products/sys_select_items.aspx?ids="+ document.getElementById("hidd_ids").value; oDialog.show(url, 720, 800, true, selectItemsCallBack);}function selectItemsCallBack(returnValue) { if (returnValue != null) { //做數據操作 }}sys_select_items.aspx彈窗頁面
//確定按鈕function btnSubmit_onclick() { var returnValue = "Y"; oDialog.sendMessage(returnValue);}
0 0
- Chrome37+代替window.showModalDialog模态窗口方法
- 模态窗口window.showModalDialog
- 模态窗口window.showModalDialog
- window.open()代替window.showModalDialog()的方法
- JS解决chrome37+ window.showmodaldialog函数不能使用的问题
- web模态窗口window.showModalDialog简介
- window.showModalDialog(模态对话窗口)
- 关于模态窗口window.showModalDialog
- Web模态窗口window.showModalDialog简介
- web模态窗口window.showModalDialog简介
- window.showModalDialog模态窗口参数说明
- 模态窗口小例子window.showModalDialog
- 关于模态窗口window.showModalDialog
- 关于模态窗口window.showModalDialog
- 完全可以用window.open()代替window.showModalDialog()的方法
- window.open代替window.showModalDialog(非标准的BOM方法)
- window.open()代替window.showModalDialog()
- window.showModalDialog打开模态窗口父子页面间对话
- 自定义控件之CascadeLayout(一)
- java poi组件Excel的导入导出
- Java RandomAccessFile用法(转载)
- DirectX 11游戏编程学习笔记之2: 第1章Vector Algebra(向量代数)
- android graphic classes
- Chrome37+代替window.showModalDialog模态窗口方法
- 区分ubuntu 32bit or 64bit
- easy ui layout 高度 宽度自适应浏览器
- gradle 教程学习笔记(一)
- Matlab 随机数 小结
- hive安装与配置
- no module named pycurl 解决办法
- 棕最桌椎足妆子状篆篆孜坠子准做酌赚桩
- 25个让Java程序员更高效的Eclipse插件(转)