Technology Programming

An IE7 bug?

Unlike JavaScript where the only links between your JavaScript objects and the elements in your HTML are the ones you build yourself, JScript as used by Internet Explorer builds lots of relationships between JScript objects and HTML elements automatically. These are often the cause why a script that works quite well in any other browser will refuse to work properly in IE. Internet Explorer has linked an object that you are trying to use in your script to an element in the HTML that you didn't want it linked to.


Occasionally JScript goes even further than this in associating code with HTML elements in unexpected ways. One such example of this in IE7 is where you have a form that uses a <button> element. The associated button object in JScript automatically has its own start() method attached to it which IE uses to create the button.

If you happen to decide to have your own start function within your code and want the button to call that function when the button is pressed then onclick="start()" will work for every browser except for IE7 since every other browser will recognise that as a call to your function.

Internet Explorer will treat that code as a call to the start method on the button object that it built itself rather than to your start function and so that call will not execute your function from Internet Explorer 7.

So how do we fix it?

Well in JavaScript and JScript when running in a web browser all stand alone functions are actually methods of the window object and so window.start() and start() are normally calls to the exact same function with the second of these being the shorter and therefore the more commonly used way of calling the function.

With IE7 these two calls are not the same when it comes to a button element since start() is taken to be an internal reference to button.start() rather than window.start() when the call is attached to a button.

We can therefore fix the call without needing to change any of the other code simply by changing the code from onclick="start()" to onclick="windows.start()". This explicitly calls the start function and makes it clear to IE7 that it is the separate function that we are calling rather than the built-in method.

The alternatives to fixing it this way involve making changes to the name of the function everywhere that it appears. This particular problem relates specifically to IE7 and calls to a function called start from a button. Simply change the name of the function from start to anything else everywhere that the function name appears and that will also fix the problem. Alternatively, if there are too many references to the start function and you don't like the idea of specifically referencing the window object to get the right function to run, you can change the name of the function that the button is calling to something else and then set up a new function by that name which simply calls the start function without doing anything else.

Any of these three ways will fix the problem. Which is shortest and quickest to apply will depend on how many buttons you have that need to call the function and how many other references that there are to the function.

Leave a reply