Javascript 如何使window.showmodaldialog在Chrome37中工作?

Javascript 如何使window.showmodaldialog在Chrome37中工作?,javascript,jquery,asp.net,google-chrome,cross-browser,Javascript,Jquery,Asp.net,Google Chrome,Cross Browser,我们有一个巨大的web应用程序,其中我们使用窗口。showmodaldialog进行警报、确认和弹出窗口。自Chrome版本37以来,此呼叫已被禁用 在最新版本的Chrome中,是否有快速的解决方法使window.showmodaldialog工作 我在这里为window.showmodaldialog添加了一个变通方法,尽管这不是一个完美的变通方法,因为这不会像showmodaldialog那样中断代码执行,而是会打开弹出窗口。 window.showModalDialog=函数(url、参数

我们有一个巨大的web应用程序,其中我们使用
窗口。showmodaldialog
进行警报、确认和弹出窗口。自Chrome版本37以来,此呼叫已被禁用

在最新版本的Chrome中,是否有快速的解决方法使
window.showmodaldialog
工作

我在这里为window.showmodaldialog添加了一个变通方法,尽管这不是一个完美的变通方法,因为这不会像showmodaldialog那样中断代码执行,而是会打开弹出窗口。

window.showModalDialog=函数(url、参数、功能){
var opFeature=feature.split(“;”);
var featuresArray=新数组()
如果(全部文件){
对于(var i=0;i
只需将此代码放在页面的标题部分。

本文()似乎表明showModalDialog已被弃用

window.showModalDialog已弃用(打算删除: window.showModalDialog(),从Web上删除showModalDialog 平台)。[…]最新计划是拆除showModalDialog 在铬37。这意味着该功能将在Opera 24和24中消失 Chrome37,这两款手机都将于9月发布。[…]

这是设计上不赞成的。您可以重新启用showModalDialog支持,但只能暂时启用,直到2015年5月。利用这段时间创建替代解决方案

下面是如何在Chrome for Windows中实现这一点。打开注册表编辑器(regedit)并创建以下项:

HKEY\U LOCAL\U MACHINE\SOFTWARE\Policys\Google\Chrome\EnableDeprecatedWebPlatformFeatures

启用DeprecatedWebPlatformFeatures
键下,创建一个名为
1
且值为
ShowModalDialog\u的字符串值,有效期至20150430
。要验证策略是否已启用,请访问chrome://policy 网址。
更新:如果上述方法不适用于您,请尝试另一种方法

  • 从下载Chrome ADM模板
  • 提取和导入与您的区域设置相关的策略(例如windows\adm\en US\chrome.adm。您可以通过
    gpedit.msc
    或在windows的家庭版本上使用这些实用程序进行导入:)
  • 在“管理模板”下找到Google Chrome模板并启用“启用不推荐的Web平台Feautes”
  • 打开该功能并添加“ShowModalDialog_effectiveuntl20150430”键

  • 是的,它已被弃用。昨天花了很多时间重写代码,改用Window.open和PostMessage。

    我将以下javascript放在页面标题中,它似乎可以工作。它检测浏览器何时不支持showModalDialog,并附加一个自定义方法,该方法使用Window.open解析对话框规格(高度、宽度、滚动等),以打开器为中心,并将焦点设置回窗口(如果焦点丢失)。此外,它使用URL作为窗口名称,因此每次都不会打开新窗口。如果要将窗口参数传递给模式,则需要编写一些附加代码来解决此问题。弹出窗口不是模式的,但至少不需要更改很多代码。可能需要针对您的情况进行一些工作

    <script type="text/javascript">
      // fix for deprecated method in Chrome 37
      if (!window.showModalDialog) {
         window.showModalDialog = function (arg1, arg2, arg3) {
    
            var w;
            var h;
            var resizable = "no";
            var scroll = "no";
            var status = "no";
    
            // get the modal specs
            var mdattrs = arg3.split(";");
            for (i = 0; i < mdattrs.length; i++) {
               var mdattr = mdattrs[i].split(":");
    
               var n = mdattr[0];
               var v = mdattr[1];
               if (n) { n = n.trim().toLowerCase(); }
               if (v) { v = v.trim().toLowerCase(); }
    
               if (n == "dialogheight") {
                  h = v.replace("px", "");
               } else if (n == "dialogwidth") {
                  w = v.replace("px", "");
               } else if (n == "resizable") {
                  resizable = v;
               } else if (n == "scroll") {
                  scroll = v;
               } else if (n == "status") {
                  status = v;
               }
            }
    
            var left = window.screenX + (window.outerWidth / 2) - (w / 2);
            var top = window.screenY + (window.outerHeight / 2) - (h / 2);
            var targetWin = window.open(arg1, arg1, 'toolbar=no, location=no, directories=no, status=' + status + ', menubar=no, scrollbars=' + scroll + ', resizable=' + resizable + ', copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            targetWin.focus();
         };
      }
    </script>
    
    
    //修复Chrome37中不推荐的方法
    如果(!window.showModalDialog){
    window.showModalDialog=函数(arg1、arg2、arg3){
    var-w;
    var h;
    var resizeable=“否”;
    var scroll=“否”;
    var status=“否”;
    //获取模态规格
    var mdattrs=arg3.split(“;”);
    对于(i=0;i
    我不会尝试暂时启用不推荐使用的功能。根据MDN
    <script type="text/javascript">
      // fix for deprecated method in Chrome 37
      if (!window.showModalDialog) {
         window.showModalDialog = function (arg1, arg2, arg3) {
    
            var w;
            var h;
            var resizable = "no";
            var scroll = "no";
            var status = "no";
    
            // get the modal specs
            var mdattrs = arg3.split(";");
            for (i = 0; i < mdattrs.length; i++) {
               var mdattr = mdattrs[i].split(":");
    
               var n = mdattr[0];
               var v = mdattr[1];
               if (n) { n = n.trim().toLowerCase(); }
               if (v) { v = v.trim().toLowerCase(); }
    
               if (n == "dialogheight") {
                  h = v.replace("px", "");
               } else if (n == "dialogwidth") {
                  w = v.replace("px", "");
               } else if (n == "resizable") {
                  resizable = v;
               } else if (n == "scroll") {
                  scroll = v;
               } else if (n == "status") {
                  status = v;
               }
            }
    
            var left = window.screenX + (window.outerWidth / 2) - (w / 2);
            var top = window.screenY + (window.outerHeight / 2) - (h / 2);
            var targetWin = window.open(arg1, arg1, 'toolbar=no, location=no, directories=no, status=' + status + ', menubar=no, scrollbars=' + scroll + ', resizable=' + resizable + ', copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
            targetWin.focus();
         };
      }
    </script>
    
    function handleReturnValue(returnValue) {
        if (returnValue !== undefined) {
            // do what you want
        }
    }
    
    var myCallback = function (returnValue) { // callback for chrome usage
        handleReturnValue(returnValue);
    };
    
    var returnValue = window.showModalDialog('someUrl', 'someDialogTitle', 'someDialogParams', myCallback); 
    handleReturnValue(returnValue); // for other browsers except Chrome
    
    function _showModalDialog(url, width, height, closeCallback) {
        var modalDiv,
            dialogPrefix = window.showModalDialog ? 'dialog' : '',
            unit = 'px',
            maximized = width === true || height === true,
            w = width || 600,
            h = height || 500,
            border = 5,
            taskbar = 40, // windows taskbar
            header = 20,
            x,
            y;
    
        if (maximized) {
            x = 0;
            y = 0;
            w = screen.width;
            h = screen.height;
        } else {
            x = window.screenX + (screen.width / 2) - (w / 2) - (border * 2);
            y = window.screenY + (screen.height / 2) - (h / 2) - taskbar - border;
        }
    
        var features = [
                'toolbar=no',
                'location=no',
                'directories=no',
                'status=no',
                'menubar=no',
                'scrollbars=no',
                'resizable=no',
                'copyhistory=no',
                'center=yes',
                dialogPrefix + 'width=' + w + unit,
                dialogPrefix + 'height=' + h + unit,
                dialogPrefix + 'top=' + y + unit,
                dialogPrefix + 'left=' + x + unit
            ],
            showModal = function (context) {
                if (context) {
                    modalDiv = context.document.createElement('div');
                    modalDiv.style.cssText = 'top:0;right:0;bottom:0;left:0;position:absolute;z-index:50000;';
                    modalDiv.onclick = function () {
                        if (context.focus) {
                            context.focus();
                        }
                        return false;
                    }
                    window.top.document.body.appendChild(modalDiv);
                }
            },
            removeModal = function () {
                if (modalDiv) {
                    modalDiv.onclick = null;
                    modalDiv.parentNode.removeChild(modalDiv);
                    modalDiv = null;
                }
            };
    
        // IE
        if (window.showModalDialog) {
            window.showModalDialog(url, null, features.join(';') + ';');
    
            if (closeCallback) {
                closeCallback();
            }
        // Other browsers
        } else {
            var win = window.open(url, '', features.join(','));
            if (maximized) {
                win.moveTo(0, 0);
            }
    
            // When charging the window.
            var onLoadFn = function () {
                    showModal(this);
                },
                // When you close the window.
                unLoadFn = function () {
                    window.clearInterval(interval);
                    if (closeCallback) {
                        closeCallback();
                    }
                    removeModal();
                },
                // When you refresh the context that caught the window.
                beforeUnloadAndCloseFn = function () {
                    try {
                        unLoadFn();
                    }
                    finally {
                        win.close();
                    }
                };
    
            if (win) {
                // Create a task to check if the window was closed.
                var interval = window.setInterval(function () {
                    try {
                        if (win == null || win.closed) {
                            unLoadFn();
                        }
                    } catch (e) { }
                }, 500);
    
                if (win.addEventListener) {
                    win.addEventListener('load', onLoadFn, false);
                } else {
                    win.attachEvent('load', onLoadFn);
                }
    
                window.addEventListener('beforeunload', beforeUnloadAndCloseFn, false);
            }
        }
    }
    
        (function() {
            window.spawn = window.spawn || function(gen) {
                function continuer(verb, arg) {
                    var result;
                    try {
                        result = generator[verb](arg);
                    } catch (err) {
                        return Promise.reject(err);
                    }
                    if (result.done) {
                        return result.value;
                    } else {
                        return Promise.resolve(result.value).then(onFulfilled, onRejected);
                    }
                }
                var generator = gen();
                var onFulfilled = continuer.bind(continuer, 'next');
                var onRejected = continuer.bind(continuer, 'throw');
                return onFulfilled();
            };
            window.showModalDialog = window.showModalDialog || function(url, arg, opt) {
                url = url || ''; //URL of a dialog
                arg = arg || null; //arguments to a dialog
                opt = opt || 'dialogWidth:300px;dialogHeight:200px'; //options: dialogTop;dialogLeft;dialogWidth;dialogHeight or CSS styles
                var caller = showModalDialog.caller.toString();
                var dialog = document.body.appendChild(document.createElement('dialog'));
                dialog.setAttribute('style', opt.replace(/dialog/gi, ''));
                dialog.innerHTML = '<a href="#" id="dialog-close" style="position: absolute; top: 0; right: 4px; font-size: 20px; color: #000; text-decoration: none; outline: none;">&times;</a><iframe id="dialog-body" src="' + url + '" style="border: 0; width: 100%; height: 100%;"></iframe>';
                document.getElementById('dialog-body').contentWindow.dialogArguments = arg;
                document.getElementById('dialog-close').addEventListener('click', function(e) {
                    e.preventDefault();
                    dialog.close();
                });
                dialog.showModal();
                //if using yield
                if(caller.indexOf('yield') >= 0) {
                    return new Promise(function(resolve, reject) {
                        dialog.addEventListener('close', function() {
                            var returnValue = document.getElementById('dialog-body').contentWindow.returnValue;
                            document.body.removeChild(dialog);
                            resolve(returnValue);
                        });
                    });
                }
                //if using eval
                var isNext = false;
                var nextStmts = caller.split('\n').filter(function(stmt) {
                    if(isNext || stmt.indexOf('showModalDialog(') >= 0)
                        return isNext = true;
                    return false;
                });
                dialog.addEventListener('close', function() {
                    var returnValue = document.getElementById('dialog-body').contentWindow.returnValue;
                    document.body.removeChild(dialog);
                    nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\(.*\)/g, JSON.stringify(returnValue));
                    eval('{\n' + nextStmts.join('\n'));
                });
                throw 'Execution stopped until showModalDialog is closed';
            };
        })()
    
    ;
    
    **Explanation:
    ------------**
    The best way to deal with showModalDialog for older application conversions is use to `https://github.com/niutech/showModalDialog` inorder to work with show modal dialogs  and if modal dailog has ajax calls you need to create object and set the parameters of function to object and pass below...before that check for browser and set the useragent...example: agentStr = navigator.userAgent; and then check for chrome
    
    var objAcceptReject={}; // create empty object and set the parameters to object and send to the other functions as dialog when opened in chrome breaks the functionality
        function rejectClick(index, transferId) {
            objAcceptReject.index=index;
            objAcceptReject.transferId=transferId;
    
         agentStr = navigator.userAgent;
    
                    var msie = ua.indexOf("MSIE ");
    
                    if (msie > 0) // If Internet Explorer, return version number
                    {
                        var ret = window.showModalDialog("/abc.jsp?accept=false",window,"dialogHeight:175px;dialogWidth:475px;scroll:no;status:no;help:no");   
    
                        if (ret=="true") {
                            doSomeClick(index);
                        }
    
                    } else if ((agentStr.indexOf("Chrome")) >- 1){
                    spawn(function() {
    
                        var ret = window.showModalDialog("/abcd.jsp?accept=false",window,"dialogHeight:175px;dialogWidth:475px;scroll:no;status:no;help:no");   
    
                        if (ret=="true") {// create an object and store values in objects and send as parameters
                            doSomeClick(objAcceptReject.index);
                        }
    
                    });
    
                    }
                    else {
                        var ret = window.showModalDialog("/xtz.jsp?accept=false",window,"dialogHeight:175px;dialogWidth:475px;scroll:no;status:no;help:no");   
    
                        if (ret=="true") {
                            doSomeClick(index);
                        }
                    }
    
    var answer = window.showModalDialog(<your page and other arguments>)
    if (answer == 1)
     { do some thing with answer }
    
    window.returnValue = 'value that you want to return';
    
    function openDialog(url, width, height, callback){
    if(window.showModalDialog){
        options = 'dialogHeight: '+ height + '; dialogWidth: '+ width + '; scroll=no'
        var returnValue = window.showModalDialog(url,this,options);
        callback(returnValue)
    }
    else {
        options ='toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=no, width=' + width + ', height=' + height; 
            var childWindow = window.open(url,"",options);
            $(childWindow).on('unload',function(){
                if (childWindow.isOpened == null) {
                    childWindow.isOpened = 1;
                }
                else {
                    if(callback){
                        callback(childWindow.returnValue);
                    }
                }
            });
    }
    
    function callback(returnValue){
    if(returnValue){
        do something nice with the returnValue
    }}
    
    openDialog(<your page>, 'width px', 'height px', callbak);