为什么 JavaScript 只能在 IE 中打开开发者工具一次后才能工作? [英] Why does JavaScript only work after opening developer tools in IE once?

查看:12
本文介绍了为什么 JavaScript 只能在 IE 中打开开发者工具一次后才能工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IE9 错误 - JavaScript 仅在打开开发者工具一次后才有效.

IE9 Bug - JavaScript only works after opening developer tools once.

我们的网站为用户提供免费的pdf下载,并有简单的输入密码下载"功能.但是,它在 Internet Explorer 中根本不起作用.

Our site offers free pdf downloads to users, and it has a simple "enter password to download" function. However, it doesn't work at all in Internet Explorer.

您可以在这个示例中亲眼看看.

You can see for yourself in this example.

下载通行证是makeuseof".在任何其他浏览器中,它都可以正常工作.在 IE 中,两个按钮什么都不做.

The download pass is "makeuseof". In any other browser, it works fine. In IE, both buttons do nothing.

我发现的最奇怪的事情是,如果您使用 F12 打开和关闭开发人员工具栏,它会突然开始工作.

The most curious thing I've found is that if you open and close the developer toolbar with F12, it all suddenly starts to work.

我们已经尝试过兼容模式等,没有任何区别.

We've tried compatibility mode and such, nothing makes a difference.

如何在 Internet Explorer 中实现此功能?

How do I make this work in Internet Explorer?

推荐答案

听起来您的 javascript 中可能有一些调试代码.

It sounds like you might have some debugging code in your javascript.

您所描述的体验是包含 console.log() 或任何其他 console 功能的典型代码.

The experience you're describing is typical of code which contain console.log() or any of the other console functionality.

console 对象仅在开发工具栏打开时激活.在此之前,调用控制台对象将导致它被报告为 undefined.打开工具栏后,控制台将存在(即使工具栏随后关闭),因此您的控制台调用将起作用.

The console object is only activated when the Dev Toolbar is opened. Prior to that, calling the console object will result in it being reported as undefined. After the toolbar has been opened, the console will exist (even if the toolbar is subsequently closed), so your console calls will then work.

有几种解决方案:

最明显的方法是通过您的代码删除对 console 的引用.无论如何,你不应该在生产代码中留下这样的东西.

The most obvious one is to go through your code removing references to console. You shouldn't be leaving stuff like that in production code anyway.

如果您想保留控制台引用,您可以将它们包装在 if() 语句中,或者在尝试调用它之前检查控制台对象是否存在的其他条件.

If you want to keep the console references, you could wrap them in an if() statement, or some other conditional which checks whether the console object exists before trying to call it.

这篇关于为什么 JavaScript 只能在 IE 中打开开发者工具一次后才能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆