简体   繁体   English

catch 块中的 Javascript“警报”功能仅在 chrome 中打开开发工具时才有效

[英]Javascript 'alert' function inside catch block only works when dev tools are opened in chrome

Hello StackOverflow community!你好 StackOverflow 社区!

I've encountered a very weird problem and couldn't find any useful information on how to solve it.我遇到了一个非常奇怪的问题,找不到有关如何解决它的任何有用信息。

Somehow, a piece of javascript code works only when the dev tools window is opened (docked or as a separate window) in google chrome.不知何故,只有当开发工具窗口在谷歌浏览器中打开(停靠或作为单独的窗口)时,一段 javascript 代码才有效。

The original problem : Due to our application structure, we need to open multiple popups automatically when a page is served.原问题:由于我们的应用程序结构,我们需要在提供页面时自动打开多个弹出窗口。 Since the popups are NOT opened through a direct user interaction (like onclick), modern browsers would automatically block these popups.由于弹出窗口不是通过直接的用户交互(如 onclick)打开的,现代浏览器会自动阻止这些弹出窗口。 Because of the large amount of code that would need to be refactored to avoid this, our solution was:由于需要重构大量代码以避免这种情况,我们的解决方案是:

  • check if the browser is blocking some popups.检查浏览器是否阻止了某些弹出窗口。
  • if so: inform the user about this and suggest to turn off their browser's popup blocking function for our website (by adding it to the exception list for example).如果是这样:通知用户这一点并建议关闭他们浏览器的我们网站的弹出窗口阻止功能(例如将其添加到例外列表中)。

Not a very elegant solution I know, but there was no other way so please don't comment on how to do this differently.我知道这不是一个非常优雅的解决方案,但没有其他方法,所以请不要评论如何以不同的方式执行此操作。

The javascript code: javascript代码:

let popupBlockingErrorShown = false;

this.OpenWindow = function (url, name, args) {

        var i = Popups.length; //Popups is an array defined as a global variable that keeps track of all 
                               //opened popup windows

        Popups[i] = window.open(url, name, args);

        try {

            Popups[i].focus();

        } catch (e) {

            if (!popupBlockingErrorShown) {

                alert("very user friendly message explaining to turn of popup blocking");

                popupBlockingErrorShown = true;
            }


        };
    }

The windows have to be popups.窗口必须是弹出窗口。 The popupBlockingErrorShown variable is to prevent having an alert message for each popup. popupBlockingErrorShown变量是为了防止每个弹出窗口都有alert消息。

Works fine in firefox.在 Firefox 中运行良好。 But in google chrome there is this behaviour:但是在谷歌浏览器中有这种行为:

  • without dev tools open : the first popup opens normally, the others are blocked, there is no alert message .没有打开开发工具:第一个弹出窗口正常打开,其他弹出窗口被阻止,没有警报消息

  • with dev tools open : the first popup opens but gets 'stuck' on loading (it's an empty page).打开开发工具:第一个弹出窗口打开但在加载时“卡住”(这是一个空页面)。 The alert message shows normally.警报消息正常显示。

Keeping the browser-window open and simply switching between dev tools opened or closed gives the same behaviour.保持浏览器窗口打开并简单地在打开或关闭的开发工具之间切换会产生相同的行为。

Anyone can help me?任何人都可以帮助我吗? Much appreciated!非常感激!

This is my first stackoverflow question and I'm still very new to programming, I have a bit over a year of experience.这是我的第一个 stackoverflow 问题,我对编程还是很陌生,我有一年多的经验。 Remarks on my 'asking questions'-skills are welcome.欢迎评论我的“提问”技巧。

Ok thanks to wOxxOm's comment I've found a workaround.好的,感谢 wOxxOm 的评论,我找到了一个解决方法。 So the problem was related to what window was focused on.所以问题与关注哪个窗口有关。 I've added a piece of code in the catch-block to show an alert on a successfully opened popup (if there is one) :我在catch-block添加了一段代码来在成功打开的弹出窗口中显示警报(如果有的话):

   try {

           Popups[i].focus();

        } catch (e) {

            if (!popupBlockingErrorShown) {
                if (Popups[i - 1]) { //there is a previous popup and it's been focused on.
                    Popups[i - 1].alert(UIMessages[33]); //show alert on opened popup.
                    popupBlockingErrorShown = true;
                }
                else {
                    alert(UIMessages[33]);
                    popupBlockingErrorShown = true;
                }
            }
        }

Thanks @wOxxOm !谢谢@wOxxOm!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM