Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this browser-dependent javascript code?

Tags:

javascript

Why different output in IE and FF?
In IE its showing : Hello and In FF its showing : Hi

var message = "Hi";
setTimeout(function(){alert(message);},10);
setTimeout(function(){message = "Hello";},0);

what is standarad? which browser is doing it right?

Note: if i convert 10 to 11 in FF then it shows Hello

like image 700
coure2011 Avatar asked Jul 28 '10 06:07

coure2011


People also ask

Is JavaScript browser dependent?

Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or the CPU, because it was initially created for browsers which do not require it. JavaScript's capabilities greatly depend on the environment it's running in.

How do you detect which browser is being used JavaScript?

How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ? The browser on which the current page is opening can be checked using JavaScript. The userAgent property of the navigator object is used to return the user-agent header string sent by the browser.

Is JavaScript the same in every browser?

It's important to know that each browser has its own tooling, including its own version of JavaScript based on the ECMAscript standards. Yes, that's right. ECMA only sets the standards for JavaScript, and every browser has its own version of JavaScript.


2 Answers

Firefox handles small delays differently to IE. Firefox have a min delay time of 10ms (which isn't exact either). See the notes of https://developer.mozilla.org/en/window.setTimeout for more info.

like image 107
Ben Rowe Avatar answered Sep 22 '22 08:09

Ben Rowe


On my PC I ran it in both FF and IE, and I had exactly the opposite results.

The reason for this is your timeout is just 10 milliseconds long. The resolution of Timers on Windows is actually about 10ms, so it's possible that either timeout could happen first. To be really sure that one thing happens before the other, you should definitely have a wider gap between timeouts.

And even then, you shouldn't expect it to always work :-)

If you really want to do things in the same order, keep it in the same line of code, or set flags saying whether or not a particular action has been completed, and check that before doing a second one which relies on the first.

like image 33
Vincent McNabb Avatar answered Sep 20 '22 08:09

Vincent McNabb