Why does JavaScript only work after opening developer tools in IE once?
It sounds like you might have some debugging code in your javascript.
The experience you're describing is typical of code which contain console.log()
or any of the other console
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 your console calls will then work.
There are a few solutions to this:
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 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.
Weird IE, Javascript only works in development mode (F12)
Taken from the comments on the original question:
Why does JavaScript only work after opening developer tools in IE once?
For OP it was the caching of his Ajax request causing the issue. Solved by disabling the cache:
$.ajax({cache: false, ...})
Javascript running only once on Internet Explorer, runs properly when developer tools is open
(I put in comments but will make this an answer now as it appears to have solved the problem) get requests are cached by IE but when the developer console is open it does not perform this cache.
three ways to fix:
add a timestamp to the request to trick the browser into thinking it is making a new request each time
var requestUrl = '/Tools.ashx?command=validscreen&time='+new Date().getTime();
set the response header to no-cache
- make a POST request as these are not cached
(as pointed out by @juanmendes not ideal you are not editing a resource)
Javascript code only works in Chrome while Chrome Developer Tools are open
You have incorrect calculations for xy, which goes beyond canvas.
If your canvas is located down in the page with the scroll
canvas.offsetTop
would be amount the space from the start of the scroll (grows to infinity when your scroll grows), but e.clientY
would grow to window height (display height) which is a fix number.
I would suggest using mouseEvent.offsetX which is supported in ie9+ and test it in different conditions on different browsers. I tested it in Chrome and it works perfect w/ and w/o scroll.
const findxy = (res, e) => {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.offsetX;
currY = e.offsetY;
flag = true;
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.offsetX;
currY = e.offsetY;
draw();
}
}
}
Related Topics
How to Check If a String Is a Valid Number
How to Efficiently Count the Number of Keys/Properties of an Object in JavaScript
Refresh Image With a New One At the Same Url
Parse Query String in JavaScript
How to Insert an Element After Another Element in JavaScript Without Using a Library
Node.Js - Syntaxerror: Unexpected Token Import
Using Jquery $(This) With Es6 Arrow Functions (Lexical This Binding)
How to Check For "Undefined" in JavaScript
JavaScript: Client-Side Vs. Server-Side Validation
What Does "JavaScript:Void(0)" Mean
Difference Between Null and Undefined in JavaScript
Parsing a String to a Date in JavaScript
"Cross Origin Requests Are Only Supported For Http." Error When Loading a Local File
How to Detect Safari, Chrome, Ie, Firefox and Opera Browsers