【问题标题】:TypeError: undefined is not an object (evaluating 'newWindow.focus')TypeError:未定义不是对象(评估'newWindow.focus')
【发布时间】:2016-09-02 18:56:19
【问题描述】:

我正在使用以下 javascript 打开一个具有大小的子窗口并将其对齐到屏幕的中心

function PopupCenter(url, title, w, h) {
            // Fixes dual-screen position                         Most browsers      Firefox
            var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
            var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

            var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
            var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

            var left = ((width / 2) - (w / 2)) + dualScreenLeft;
            var top = ((height / 2) - (h / 2)) + dualScreenTop;
            var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

            // Puts focus on the newWindow
            if (window.focus) {
                newWindow.focus();
            }

            var timer = setInterval(checkChild, 500);
                function checkChild() {
                    if (newWindow.closed) {
                        var message = 'You have aborted the transaction by closing the window !';
                        var status = 'closed';
                        paymentResponseDisplay(message,status)   
                        clearInterval(timer);
                    }
            }
            window.onbeforeunload = function() {
                if(!newWindow.closed){
                    return "Do you really want to abort this transaction?";
                }
            }

        }    

但在野生动物园中我得到了

TypeError: undefined is not an object (evaluating 'newWindow.focus')

我无法找到解决此问题的方法。有人可以帮忙吗?

【问题讨论】:

  • 我不认为我可以,但是您是否通过控制台测试了您的 newWindow var?您是否使用参数最少的简单窗口进行了测试?看看这是否有所作为
  • 试过还是一样
  • 它实际上可能由于浏览器限制而失败:developer.mozilla.org/en-US/docs/Web/API/Window/focus

标签: javascript jquery html safari cross-browser


【解决方案1】:

在我的情况下,出现这个问题是因为我从 Safari 中的 ajax 结果调用了弹出窗口。 我收到了带有同步的 ajax,它运行良好。

window.open() works different on AJAX success

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2021-12-01
    • 2019-11-16
    • 2020-02-09
    • 2019-12-07
    • 2022-01-01
    • 2019-01-20
    • 2014-06-13
    • 2021-07-24
    相关资源
    最近更新 更多