Search code examples
javascriptsizzle

getAttribute() not working with Sizzle in IE 11


I have some Javascript code that works fine on every other browser but (of course) IE. Could someone tell me if there is a problem with my code, and if there is not, suggest a fix?

Note: IE returns null for href attribute.

for(var a=Sizzle("*"),i=0;i<a.length;i++) {
  a[i].onclick=function(){
    window.open(this.getAttribute("href"),"_self");
  }
}

Solution

  • Try selecting only the elements that actually have an href property.

    Something like:

    for(var a=Sizzle("[href]"), i=0; i<a.length;i++) { ... }
    

    The original cause of your problem is probably due to how event order is handled. See here for some background. You were adding event handlers to not just the links that actually have href properties but all their parents.

    Either way there aren't too many cases for using the * selector versus something more specific. And you should, generally, use the most specific selector you can.