Mario Parra
Mario Parra

Reputation: 1554

Add iPhone-detection to jQuery script

I've written a script in jQuery, which adds padding to an element when my website loads as a web app:

<script type='text/javascript'>
    jQuery("div").ready(function(){
        if (("standalone" in window.navigator) && window.navigator.standalone) {
            jQuery(".menu-secondary").css("paddingTop", "20px");
        };
    });
</script>

I'd like to extend the script to only apply when the website is loaded as a web app on an iPhone or iPod.

I've tried the following with no luck:

<script type='text/javascript'>
    jQuery("div").ready(function(){
        if (("standalone" in window.navigator) && window.navigator.standalone) ||
        ((navigator.userAgent.match(/iphone|ipod/i))) {
            jQuery(".menu-secondary").css("paddingTop", "20px");
        };
    });
</script>

Any help would be really appreciated!

Upvotes: 0

Views: 76

Answers (1)

Fede
Fede

Reputation: 41

window.navigator.userAgent.match("iPhone")

this returns an array you should check for null or length

Upvotes: 1

Related Questions