简体   繁体   English

Javascript:为什么有时 alert() 不起作用但 console.log() 起作用?

[英]Javascript: Why sometimes alert() does not work but console.log() does?

From time to time, I face a very intriguing bug.有时,我会遇到一个非常有趣的错误。 My javascript code does not display an alert(msg) during execution, but if I use a console.log(msg) it does show up in the console.我的 javascript 代码在执行期间不显示警报(消息),但如果我使用 console.log(msg),它会显示在控制台中。 What could prevent alert() from displaying?什么可以阻止 alert() 显示?

Thanks a lot非常感谢

This is a very common problem, and everyone has faced this problem atleast once.这是一个非常普遍的问题,每个人都至少遇到过一次这个问题。 The reason alert() does not work is because previously you have checked "prevent this page from creating additional dialoug" checkbox. alert() 不起作用的原因是之前您已选中“防止此页面创建其他对话框”复选框。

lets take a look at this code.让我们来看看这段代码。

<script type="text/javascript">

var js_name = ['elem1', 'elem2']

 for (var i = 0; i < js_name.length; i++) {
    alert(js_name[i]);
 };

</script>

There will be two alert boxes if you run the code.如果您运行代码,将会有两个警告框。 If you check the "prevent this page from creating additional dialoug" checkbox and then refresh the page again you won't get alert box ever again.如果您选中“阻止此页面创建其他对话框”复选框,然后再次刷新页面,您将不会再收到警告框。

Solution is you need to close that webpage and reopen again in the browser(don't need to close the entire browser).解决方案是您需要关闭该网页并在浏览器中重新打开(不需要关闭整个浏览器)。 I am assuming you are using chrome.我假设您使用的是 chrome。 Internet Explorer or FireFox doesn't have this checkbox feature. Internet Explorer 或 FireFox 没有此复选框功能。

If you override alert function so it won't work如果您覆盖警报功能,则它将无法正常工作

alert = function() 
{
 ...
};

alert('hello') // won't show any alert

To my knowledge alert() is always shown unless it is repetitive in which case you are asked if you want to continue showing alerts.据我所知,alert() 总是会显示,除非它是重复的,在这种情况下,系统会询问您是否要继续显示警报。

I suppose the specifics on how this is handled depends on your browser.我想如何处理的具体细节取决于您的浏览器。 Care to share any more details?愿意分享更多细节吗? :) :)

This also happens in ColdFusion.这也发生在 ColdFusion 中。 If you use anywhere after the script tag a cflocation tag (instead of location.href) the alert will not show.如果在脚本标记之后的任何地方使用 cflocation 标记(而不是 location.href),警报将不会显示。

This can also happen in Firefox if you have Dev Tools open and Responsive Design Mode enabled.如果您打开了开发工具并启用了响应式设计模式,这也可能发生在 Firefox 中。 It sounds like it's a bug .这听起来像是一个错误

In Firefox: go to Options -> Content and uncheck "block pop-up windows" check box.在 Firefox 中:转到选项 -> 内容并取消选中“阻止弹出窗口”复选框。 Restart browser.重新启动浏览器。

Another reason why alert , confirm , and prompt may be ignored by the browser, is if the document is in an iframe that has a sandbox -attribute without allow-modals in its value.浏览器可能会忽略alertconfirmprompt另一个原因是,如果文档位于具有sandbox属性的iframe ,而其值中没有allow-modals

For example, Firefox silently ignores this, however Chromium shows a warning.例如,Firefox 会默默地忽略这一点,但 Chromium 会显示警告。

If you try to execute javascript alert function in a Chrome browser address URL, you will not get the message if the the tab has no page previously loaded.如果您尝试在 Chrome 浏览器地址 URL 中执行 javascript 警报功能,如果该选项卡之前没有加载页面,您将不会收到消息。 You will get the alert box only if it is not a fresh tab.只有当它不是新标签时,您才会收到警报框。 If there exists a web page that is previously loaded, and then you try to run the javascript in the address bar, you will get the expected result.如果存在之前加载的网页,然后尝试在地址栏中运行javascript,您将获得预期的结果。 Hope it clarifies this difficult to detect behavior in Chrome.希望它能澄清 Chrome 中这种难以检测的行为。

If you have a location.reload() after the alert() function as shown below, JavaScript somehow skips the alert and reloads the page.如果您在 alert() function 之后有一个 location.reload() ,如下所示,JavaScript 会以某种方式跳过警报并重新加载页面。

     $.ajax({
            url:'xxx',
            type : 'POST',
            data : datavalues,
            dataType: 'json',
            contentType: 'application/json',
            success: function(response){
                if(response.status == '200')
                    {
                    alert("updated");
                    }
                    
            }
        });
     
     location.reload();

To tackle this situation, I moved the reload function into the if block and this solved the issue.为了解决这种情况,我将重新加载 function 移到了 if 块中,这解决了问题。

    $.ajax({
        url:'xxx',
        type : 'POST',
        data : datavalues,
        dataType: 'json',
        contentType: 'application/json',
        success: function(response){
            if(response.status == '200')
            {
                alert("updated");
                location.reload();
            }

        }
    });

 

I have a similar problem here, when I replace the console log with "alert" it is not working but console.log does work.我在这里有一个类似的问题,当我用“警报”替换控制台日志时,它不起作用,但console.log确实起作用。

The not working code is :不工作的代码是:

request(options, function(error, response, body) { // Requesting API

    var statusCode = response.statusCode;

    if(statusCode === 200){
      alert("Success");
    } else {
      alert(error);
    }

and the working code is:工作代码是:

request(options, function(error, response, body) { // Requesting API

    var statusCode = response.statusCode;

    if(statusCode === 200){
      console.log("Success");
    } else {
      console.log(error);
    }

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

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