Larry Martell Larry Martell - 1 year ago 173
HTML Question

javascript works on all browsers except IE

I have some web pages that load an external JavaScript file like this:

<script src="sorttable.js"></script>

This package comes from here: sorttable

I reference it in an onload function like this:

<script type="text/javascript">
window.onload = function() { sorttable.innerSortFunction.apply(document.getElementById("Symbol-2"), []); }

This works perfectly on Firefox and Chrome, but on IE version 9.0.2 it fails with these messages:

HTML1113: Document mode restart from IE9 Standards to Quirks
SEC7111: HTTPS security is compromised by javascript:void(0)
SCRIPT5007: Unable to get value of the property 'apply': object is null or undefined

This is an internal website, and 9.0.2 is the version my company deploys, and I cannot upgrade to a newer version.

Can I make this work on IE as well as the other browsers?

Answer Source

It looks like the SortTable library is using some sort of hacky browser detection in an attempt to initialize the library at the earliest possible time:

(excerpt from library source code)

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            sorttable.init(); // call the onload handler
/*@end @*/

It looks like IE is rejecting this because of the attempt to use a script with the URL javascript:void(0) on a page accessed over HTTPS.

The library also has a catchall to use the onload handler if it doesn't have a browser-specific approach for the initialization:

window.onload = sorttable.init;

but you are overwriting the onload handler with your own, so this never executes.

I think the simplest solution is just to modify your onload handler to perform the initialization:

window.onload = function() { 
    sorttable.innerSortFunction.apply(document.getElementById("Symbol-2"), []); 

and you should be all set. The init() method has an internal check to prevent it from performing the initialization twice, so you don't need to worry about issues from calling it if it has already been called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download