2012-08-17 71 views
1

可能重复:
Detect if device is iOS仅当设备基于iOS时才能运行jQuery代码?

这是我想做的事:

<script type="text/javascript"> 
     // Run This code if device is iOS based 
     window.addEventListener('DOMContentLoaded',function() { 
      $("body").queryLoader2({ 
       barColor: "#37201b", 
       backgroundColor: "#c2a875", 
       percentage: true, 
       barHeight: 5, 
       completeAnimation: "grow" 
      }); 
     }); 
     // if it is not iOS run this code 
     $(document).ready(function() { 
      $("body").queryLoader2({ 
       barColor: "#37201b", 
       backgroundColor: "#c2a875", 
       percentage: true, 
       completeAnimation: "grow", 
       barHeight: 5 
      }); 
     }); 
     </script> 

相反的意见我怎么写,使其工作?

+0

更好的问题:为什么你只想在iOS上运行,你应该使用功能检测而不是操作系统检测? – Blazemonger 2012-08-17 13:16:52

+0

@Blazemonger no no,我只需要一部分来检测iOS – Ilja 2012-08-17 13:20:10

回答

3
jQuery(document).ready(function($){ 
    var deviceAgent = navigator.userAgent.toLowerCase(); 
    var agentID = deviceAgent.match(/(iPad|iPhone|iPod)/i); 
    if (agentID) {  
     window.addEventListener('DOMContentLoaded',function() { 
      $("body").queryLoader2({ 
       barColor: "#37201b", 
       backgroundColor: "#c2a875", 
       percentage: true, 
       barHeight: 5, 
       completeAnimation: "grow" 
      }); 
     });  
    } 
    else 
    { 
     $(document).ready(function() { 
      $("body").queryLoader2({ 
       barColor: "#37201b", 
       backgroundColor: "#c2a875", 
       percentage: true, 
       completeAnimation: "grow", 
       barHeight: 5 
      }); 
     }); 
    } 
}); 
相关问题