A-A+

Jquery 判断浏览器类型的例子

2016年01月12日 前端设计 暂无评论 阅读 5 views 次

如果我们要判断浏览器类型是可以通过navigator.userAgent命令来获得里面的头部信息的,下面来看一个例子,具体如下:

  1. $(document).ready(function()  
  2. {  
  3.     var explorer =navigator.userAgent ;  
  4.     //ie  
  5.     if (explorer.indexOf("MSIE") >= 0)   
  6.     {  
  7.     }  
  8.     //<a href="/tags.php/firefox/" target="_blank">firefox</a>  
  9.     else if (explorer.indexOf("Firefox") >= 0)   
  10.     {  
  11.     }  
  12.     //Chrome  
  13.     else if(explorer.indexOf("Chrome") >= 0)  
  14.     {  
  15.         //style="margin-top: 510px; !important;  
  16.         $(".wqx").css('margin-top','520px'); 
  17.     } 
  18.     //Opera 
  19.     else if(explorer.indexOf("Opera") >= 0) 
  20.     { 
  21.     } 
  22.     //Safari 
  23.     else if(explorer.indexOf("Safari") >= 0) 
  24.     { 
  25.         $(".wqx").css('margin-top','510px'); 
  26.     } 
  27.     //Netscape 
  28.     else if(explorer.indexOf("Netscape")>= 0)   
  29.     {  
  30.     }  
  31. })  
  32. </script>  

方法二(在高版本的jquery中已经没有了$.browser.['浏览器关键字'] 的做法了)

  1. $.browser.['浏览器关键字']  
  2.   
  3. $(function() {   
  4.    
  5. if($.browser.msie) {   
  6.    
  7. alert("this is msie");   
  8.    
  9. }   
  10.    
  11. else if($.browser.safari)   
  12.    
  13. {   
  14.    
  15. alert("this is safari!");   
  16.    
  17. }   
  18.    
  19. else if($.browser.mozilla)   
  20.    
  21. {   
  22.    
  23. alert("this is mozilla!");   
  24.    
  25. }   
  26.    
  27. else if($.browser.opera) {   
  28.    
  29. alert("this is opera");   
  30.    
  31. }   
  32.    
  33. else {   
  34.    
  35. alert("i don't konw!");   
  36.    
  37. }  

方法三,如果是高版本的jquery我们知道是$.support替代$.browser方法了,当然我们也可以使用js做法,如下。

  1. 1 mozilla  
  2. if(/firefox/.test(navigator.userAgent.toLowerCase())){}    
  3. if(/firefox/.test(navigator.userAgent.toLowerCase())){}  
  4. 2 webkit  
  5. if(/webkit/.test(navigator.userAgent.toLowerCase())){}    
  6. if(/webkit/.test(navigator.userAgent.toLowerCase())){}  
  7. 3 opera  
  8.    
  9. [<a href="/js_a/js.html" target="_blank">javascript</a>] view plaincopyprint?  
  10. 01.if(/opera/.test(navigator.userAgent.toLowerCase())){}    
  11. if(/opera/.test(navigator.userAgent.toLowerCase())){}  
  12. 4 ie  
  13. if(/msie/.test(navigator.userAgent.toLowerCase())){}    
  14. if(/msie/.test(navigator.userAgent.toLowerCase())){}  
  15. 5 ie6  
  16. if ('<a href="/tags.php/undefined/" target="_blank">undefined</a>' == typeof(document.body.style.maxHeight)) {}    
  17. if ('undefined' == typeof(document.body.style.maxHeight)) {}  
  18.    
  19. 6 ie6-8  
  20.    
  21. if (!$.support.leadingWhitespace) {}    
  22. if (!$.support.leadingWhitespace) {}  
  23. 7 IE11的检测方法  
  24.    
  25. var ua=navigator.userAgent.toLowerCase();   
  26.      
  27. if (ua.match(/msie/) != null || ua.match(/trident/) != null) {    
  28.        
  29. //浏览器类型     
  30. browserType = "IE";    
  31. //浏览器版本     
  32. browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1];     
  33. }  
标签:

给我留言