[Solved-5 Solutions] Why does javascript only work after opening developer tools in IE once ? - JavaScript Tutorial
Problem:
- Why does javascript only work after opening developer tools in IE once ?
Solution 1:
- It sounds like we might have some debugging code in our javascript.
- The experience you're describing is typical of code which contain
console.log()
or any of the otherconsole
functionality. - 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 our console calls will then work. - There are a few Solutions to this:
- The most obvious one is to go through our code removing references to
console
. We shouldn't be leaving stuff like that in production code anyway. - If we want to keep the console references, we could wrap them in an if() statement, or some other conditional which checks whether the console object exists before trying to call it.
Solution 2:
HTML5 Boilerplate has a nice pre-made code for console problems fixing:
Solution 3:
- Here's another possible reason besides the
console.log
issue (at least in IE11): - When the console is not open, IE does pretty aggressive caching, so make sure that any
$.ajax
calls orXMLHttpRequest
calls have caching set to false. - For example:
When the developer console is open, caching is less aggressive. Seems to be a bug.
Solution 4:
This solved my problem after we made a minor change to it. we added the following in my html page in order to fix the IE9 problem:
This solution does not work on IE 11 on Windows 7 64-bit.
Solution 5:
We can add this before any tag of javascript may be solve the problem,