Wednesday, August 8, 2012

Object Expected error in Internet Explorer

This error usually happens in the following scenario. An HTML file contains an inline JavaScript to load remote.js and call a function in remote.js

<script type="text/javascript">
//<![CDATA[
    Event.observe(window, 'load', function() {
        product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint');
    });
//]]>
</script>

And in the file remote.js:
remoteFunction code.....


You would expect the output in this order:

    Before loading remote.js
    After loading remote.js
    In remote function

IE gives an error message "Object Expected" because it calls the function remoteFunction() prematurely. It calls before loading the file remote.js, which contain the declaration of remoteFunction(). This is another unexpected behavior from IE. Fortunately, it can be corrected by adding the attribute defer="defer" to second script invocation. This will specifically prevent IE from executing in order to give a correct output. Other browsers are not affected by the change.

No comments:

Post a Comment