I work on a website and I got a javascript function that doesn't work in Internet Explorer 6. I know
document.querySelector(selector)
only work in Internet Explorer 8+
What could I do to make it work in IE6?
(I don't try to make it work for the fun of it ;) )
You could use a polyfill, like this one, however still using IE6, is the IT analogue of necromancy.
The mentioned polyfill is based on polyfill.js, which can be found here, this provides polyfills for a lot of ECMA 5 functions.
I will post the current state of the script, maybe it will useful in the future (though I really hope, it won't be :) ):
I strongly encourage you not to try to support IE6 any longer.
But you can add
document.querySelector
anddocument.querySelectorAll
using this very clever trick from an Ajaxian article. The article actually gets it a bit wrong, it adds something calledquerySelector
that doesquerySelectorAll
instead. I've fixed the name here:Although I would never countenance using
for-in
like that; details and alternatives in this other answer.And by inference,
querySelector
:Note that neither of the above adds
Element#querySelector
orElement#querySelectorAll
(the versions that look only within an element), justdocument.querySelector
anddocument.querySelectorAll
. And you can't add theElement
versions on IE6 without adding them to each individual element, since IE6 doesn't support element prototypes.