从运行 JavaScript 中排除 iOS

Exclude iOS from running JavaScript

本文关键字:排除 iOS JavaScript 运行      更新时间:2023-09-26

我正在使用一篇文章中的预加载器,QueryLoader2 – 轻松预加载您的图像。在大多数桌面浏览器中运行良好,但我在 iOS 上遇到了问题。

有没有办法排除运行脚本的iOS设备而不提供其他页面?

这是我的代码:

$(document).ready(function () {
    $("body").queryLoader2({
        barColor: "#FFFFFF",
        backgroundColor: "#000000",
        percentage: true,
        barHeight: 1,
        completeAnimation: "grow",
        minimumTime: 1000
    });
});

由于某种原因,我无法让window.addEventListener与iOS一起使用。

您可以检查用户代理字符串。

if(!((navigator.userAgent.match(/iPhone/i)) || 
   (navigator.userAgent.match(/iPod/i)) ||
   (navigator.userAgent.match(/iPad/i)))) {
    // do non iOS stuff here
}

您的代码将如下所示

$(document).ready(function () {
    if(!((navigator.userAgent.match(/iPhone/i)) || 
      (navigator.userAgent.match(/iPod/i)) ||
      (navigator.userAgent.match(/iPad/i)))) {
        $("body").queryLoader2({
            barColor: "#FFFFFF",
            backgroundColor: "#000000",
            percentage: true,
            barHeight: 1,
            completeAnimation: "grow",
            minimumTime: 1000
        });
    }
});

我还会查看该页面上提供的iOS代码,看看是否可以解决您的问题。

window.addEventListener('DOMContentLoaded', function() {
    $("body").queryLoader2();
});

检查 iPhone 用户代理字符串,并根据该结果有条件地预加载图像