简体   繁体   中英

Removing Javascript for iPad / iPhone

I am having trouble with the Jquery scrollto command on iphone and ipads, it keeps flickering every time its used and gets stuck so I have decided to remove the code for these devices but am having trouble doing so. Here is the code I am trying to use to only show the javascript on a decktop browser.

<script type="text/javascript"> // <![CDATA[
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
    document.write = "<meta name=\"viewport\" content=width=1024px, minimum-scale=1.0, maximum-scale=1.0 \/>";
}
else{document.write = "<script type=\"text/javascript\">
$(document).ready(function() {$('.nav').onePageNav({begin: function() {console.log('start');}, end: function() {console.log('stop');}, scrollOffset: 30});});
</script>";} // ]]>
</script>

The dev site can be found here: dev.greendealcumbria.com

Cheers

To elaborate on my comment, why not just execute the function like below? Why dynamically create the script element when you could just execute the script?

<script type="text/javascript"> // <![CDATA[
if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
    document.write = "<meta name=\"viewport\" content=width=1024px, minimum-scale=1.0, maximum-scale=1.0 \/>";
}else{
$(document).ready(function(){
$('.nav').onePageNav({
begin: function() {
console.log('start');
}, 
end: function() {
console.log('stop');
}, 
scrollOffset: 30;
});
});
}
// ]]>
</script>

Try this.

var ua = navigator.userAgent.toLowerCase();
if ((ua.indexOf('iphone') != -1) 
     || (ua.indexOf('ipod') != -1) 
     || (ua.indexOf('ipad') != -1)) {
    document.write = "<meta name=\"viewport\" content=width=1024px, minimum-scale=1.0, maximum-scale=1.0 \/>";
}
else{
    $(document).ready(function(){
        $('.nav').onePageNav({
            begin: function() {
                console.log('start');
            }, 
            end: function() {
                console.log('stop');
            }, 
            scrollOffset: 30
        });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM