psotirov

IE7 querySelector

Apr 26th, 2013
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         if (!document.querySelectorAll) { // IE7 querySelector hack
  2.             document.querySelectorAll = function (selector) {
  3.                 var head = document.documentElement.firstChild; // takes <head> tag
  4.                 var styleTag = document.createElement("STYLE"); // creates a new <style> tag
  5.                 head.appendChild(styleTag);
  6.                 document.arrayOfSelectorNodes = [];
  7.  
  8.                 // the next line is the magic - so called IE5-IE7 CSS expressions:
  9.                 //    the expression is executed on each HTML element that fulfils the selector  
  10.                 styleTag.styleSheet.cssText = selector + "{x:expression(document.arrayOfSelectorNodes.push(this))}";
  11.                 // the next line forces DOM tree to reload and and CCS to execute
  12.                 window.scrollBy(1, 0);
  13.                 // initially was implemented window.scrollBy(0, 0) but it seems to work only in Quirks mode or probably in "original" IE7
  14.                 // this double invoke of scrollBy has side effect of doubling the result, however it works!
  15.                 head.removeChild(styleTag);
  16.                 window.scrollBy(-1, 0);
  17.                 return document.arrayOfSelectorNodes;
  18.             }
  19.         }
Advertisement
Add Comment
Please, Sign In to add comment