开发者

detecting iPad in javascript without using user-agent?

开发者 https://www.devze.com 2023-03-13 08:29 出处:网络
I have a javascript code that I don\'t want it to run on iPads. is there a way to detect an iPad using javaScript without using user-agent.

I have a javascript code that I don't want it to run on iPads.

is there a way to detect an iPad using javaScript without using user-agent.

say for example using the device width as we do it for css.

@media only screen and (device-width: 768px)开发者_开发知识库 {/* For general iPad layouts */}

Thanks in advance,

R


You can use that media query, it's a bit hacky but you can set a style attribute to a predefined element (for example your body), read that with javascript and respond to it.

HTML Head

@media only screen and (device-width: 768px) { #tester { border-color: #f00; } }

HTML Body

<body>
<div id="tester" style="display: none"></div>

JS

if(document.getElementById('tester').style.borderColor == '#f00') {
   // iPad
}

A bit hacky but you can tidy it up a bit if you really want to do it this way; I do prefer checking user agents though ;)


Does navigator.platform count as User-Agent-String? You could use:

var is_iPad = navigator.platform.indexOf("iPad") != -1 ;
0

精彩评论

暂无评论...
验证码 换一张
取 消