internet explorer - Check if the browser supports document.querySelectorAll in JavaScript - Stack Overflow

Now although most modern browser support document.querySelectorAll(), you may run into problems with ol

Now although most modern browser support document.querySelectorAll(), you may run into problems with older versions of Internet Explorer. The obvious way of checking if the browser supports a function would be:

if(document.querySelectorAll){
    //some random code
}

But from what I understand some browsers like (IE8) don't support certain properties, like 'body *'. Is there a better way to check if document.querySelectorAll('body *') will actually work?

Now although most modern browser support document.querySelectorAll(), you may run into problems with older versions of Internet Explorer. The obvious way of checking if the browser supports a function would be:

if(document.querySelectorAll){
    //some random code
}

But from what I understand some browsers like (IE8) don't support certain properties, like 'body *'. Is there a better way to check if document.querySelectorAll('body *') will actually work?

Share Improve this question edited Mar 11, 2014 at 10:57 user2428118 8,1244 gold badges46 silver badges73 bronze badges asked Mar 9, 2014 at 19:16 OodOod 1,8354 gold badges27 silver badges50 bronze badges 7
  • 1 IE8 supports CSS2 selectors only, but wouldn't body * be a CSS2 selector ? – adeneo Commented Mar 9, 2014 at 19:17
  • Exactly that's the problem I'm having. I am looking for a way to test this. – Ood Commented Mar 9, 2014 at 19:18
  • What does body * do in IE8? – Felix Kling Commented Mar 9, 2014 at 19:18
  • I don't think you understood, wouldn't body * work in IE8, it looks like a CSS2 selector to me, and should work ? – adeneo Commented Mar 9, 2014 at 19:19
  • What says the console? – Adrian Preuss Commented Mar 9, 2014 at 19:19
 |  Show 2 more ments

3 Answers 3

Reset to default 5

document.querySelectorAll will thrown on any unsupported selector so you can simply use a try-catch block.

Check browser supports or not , without try-catch :

function QuerySelectors() {
  return (document['querySelector']&&document['querySelectorAll'])!=null;
}

or

function QuerySelectors(){
  return typeof(document['querySelector'])=='function'&&typeof(document['querySelectorAll'])=='function';
}

Read more > Reference

Use typeof to check it:

 if(typeof(document.querySelectorAll) != 'undefined'){
      //some random code
 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745227480a4617530.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信