Why does JavaScript only work after opening developer tools in IE once?

前端 未结 12 2104
予麋鹿
予麋鹿 2020-11-22 02:20

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

Our site offers free pdf downloads to users, and it has a simple \"enter password to download\" f

相关标签:
12条回答
  • 2020-11-22 02:36

    I guess this could help, adding this before any tag of javascript:

    try{
      console
    }catch(e){
       console={}; console.log = function(){};
    }
    
    0 讨论(0)
  • 2020-11-22 02:41

    This solved my problem after I made a minor change to it. I added the following in my html page in order to fix the IE9 problem:

    <script type="text/javascript">
        // IE9 fix
        if(!window.console) {
            var console = {
                log : function(){},
                warn : function(){},
                error : function(){},
                time : function(){},
                timeEnd : function(){}
            }
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 02:41

    It happened in IE 11 for me. And I was calling the jquery .load function. So I did it the old fashion way and put something in the url to disable cacheing.

    $("#divToReplaceHtml").load('@Url.Action("Action", "Controller")/' + @Model.ID + "?nocache=" + new Date().getTime());
    
    0 讨论(0)
  • 2020-11-22 02:47

    We ran into this problem on IE 11 on Windows 7 and Windows 10. We discovered what exactly the problem was by turning on debugging capabilities for IE (IE > Internet Options > Advanced tab > Browsing > Uncheck Disable script debugging (Internet Explorer)). This feature is typically checked on within our environment by the domain admins.

    The problem was because we were using the console.debug(...) method within our JavaScript code. The assumption made by the developer (me) was I did not want anything written if the client Developer Tools console was not explicitly open. While Chrome and Firefox seemed to agree with this strategy, IE 11 did not like it one bit. By changing all the console.debug(...) statements to console.log(...) statements, we were able to continue to log additional information in the client console and view it when it was open, but otherwise keep it hidden from the typical user.

    0 讨论(0)
  • 2020-11-22 02:51

    Besides the 'console' usage issue mentioned in accepted answer and others,there is at least another reason why sometimes pages in Internet Explorer work only with the developer tools activated.

    When Developer Tools is enabled, IE doesn't really uses its HTTP cache (at least by default in IE 11) like it does in normal mode.

    It means if your site or page has a caching problem (if it caches more than it should for example - that was my case), you will not see that problem in F12 mode. So if the javascript does some cached AJAX requests, they may not work as expected in normal mode, and work fine in F12 mode.

    0 讨论(0)
  • 2020-11-22 02:55

    I got yet another alternative for the solutions offered by runeks and todotresde that also avoids the pitfalls discussed in the comments to Spudley's answer:

            try {
                console.log(message);
            } catch (e) {
            }
    

    It's a bit scruffy but on the other hand it's concise and covers all the logging methods covered in runeks' answer and it has the huge advantage that you can open the console window of IE at any time and the logs come flowing in.

    0 讨论(0)
提交回复
热议问题