简体   繁体   中英

Script causes IE6 to fallover with no error

I hate IE6, in fact I wish microsoft would force out a patch that killed the damn thing stone dead. The following script works fine in IE > 6 & FF,WebKit(chrome etal) without issue; any ideas?

(function getElementsByClass(searchClass) {
        node = document;
        tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        var count = 1;
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                    var re1='.*?';  // Non-greedy match on filler
                    var re2='(\\\'.*?\\\')';    // Single Quote String 1
                    var p = new RegExp(re1+re2,["i"]);
                    var m = p.exec(els[i].getAttribute("onclick"));
                    var popURL = "";
                    if (m != null)
                    {
                      var strng1=m[1];
                      popURL = strng1.replace(/'/g,'');
                    }

                    els[i].setAttribute("href", popURL + "?keepthis=true&tb_iframe=true&height=430&width=400");
                    els[i].setAttribute("class", "thickbox"); 
                    els[i].removeAttribute("onclick");
                    j++;
                    count++;
            }
        }
       // return count; Ignore the return
})("vtthickbox");

typeof els[i].getAttribute("onclick") returns function in my IE6, but FF, Opera returns string .

I don't think RegExp.exec can handle function object.

And with that, typeof m[1] became undefined , which cause popURL to undefined too.

Why not just use this instead:

function getElementsByClass(searchClass) {
 var classElements = new Array(),
     node = document,
     tag = '*',
     els = node.getElementsByTagName(tag),
     elsLen = els.length,
     pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"),
     i,
     j;
 for (i = 0, j = 0; i < elsLen; i++) {
  if (pattern.test(els[i].className)) {
   classElements[j] = els[i];
   j++;
  };
 };
 return classElements;
};

Then put the onclick event handler logic outside of the getElementsByClass() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM