JavaScript in different browsers

Posted by PointsToShare on Geeks with Blogs See other posts from Geeks with Blogs or by PointsToShare
Published on Sun, 13 Nov 2011 20:54:53 GMT Indexed on 2011/11/14 1:52 UTC
Read the original article Hit count: 461

Filed under:

Adventures with JavaScript rendered in IE 8, Chrome 15, and Firefox 8.0

I have written a little monogram about the advantages of Math and wrote a few JavaScript applications to demonstrate them.

I was a bit careless and used elements on the page in my JavaScript without using any of the GetElementsByXXXX methods to identify them.  Say I had a text box named tbSeqNum into which I entered a number to be used in a computation. In my code I simply referred to its value by using it directly. Like here:

Function Blah()
{
                return tbSeqNum.value;
}

This ran fine in IE8. In IE, the elements are available as global variables. This is not the case in either Firefox or Chrome.

In there one has to create the variable and only then use it. Assuming I also used tbSeqNum as the element’s ID, this works:

Function Blah()
{
                return GetElementById(“tbSeqNum”).value;
}

Naturally this corrected function also works in IE, so be warned.

Also, coming from windows programming (I am long in the tooth and programmed long before the internet), I have a habit of putting an “Exit” button on my pages and setting their onclick to: onclick=”window.close()”. Again, this works fine in IE. In Firefox and chrome, it does not! There you can only close a window that you opened in the code. A window that was opened by navigation to a URL will not close. 

Before I deployed mu code to my website, I painfully removed all my Exit buttons.

But my greatest surprise came when I tested my pages in the various browsers. In my code I do a comparison on the performance of two algorithms used to solve the same problem. One is brute force, the other uses a mathematical formula. The compare functions runs each many times and displays the time it took for each and also the ratio.

Chrome runs JavaScript between 5 and 10 times faster than Firefox and between 50 and 100 times faster that IE. Wow!!! This difference is especially remarkable when the code uses iteration. I suspect that the JS engines in Chrome and Firefox simply cache the result of a function and if it is called again with the same parameters, it returns the cached result.

To see it in action play run the “How Many Squares” page in www.mgsltns.com/games.htm The host is running on Unix, so the link is case sensitive.

Last Note: IE9 runs JS a bit faster, but still lags behind almost as badly.

That’s All Folks!

 

 

© Geeks with Blogs or respective owner