【问题标题】:How to load a script only in specific Browsers?如何仅在特定浏览器中加载脚本?
【发布时间】:2020-10-12 10:07:33
【问题描述】:

我想做什么: 我要加载负载 Twemoji 在除 Safari (Mobile/MacOS) 之外的所有浏览器中。 我试过用 JS 做,但是没用:

function IsSafari() {


var is_safari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 &&
               navigator.userAgent &&
               navigator.userAgent.indexOf('CriOS') == -1 &&
               navigator.userAgent.indexOf('FxiOS') == -1;
  return is_safari;

} if(!IsSafari){console.log("No reliable Emoji-set detected. Loading Twemoji from cdnjs");
$.getScript("https://cdnjs.cloudflare.com/ajax/libs/twemoji/12.0.4/2/twemoji.min.js", function() {
   twemoji.parse(document.body);
});}

【问题讨论】:

标签: javascript html jquery browser twemoji


【解决方案1】:

这里是您如何知道用户是否没有使用 safari 浏览器

if(!(/constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification)))){
    console.log('Hey I am sure that you are not using safari browser ;)');
}else{
    console.log('Apple user detected');
}

我从这个 stackoverflow 问题答案How to detect Safari, Chrome, IE, Firefox and Opera browser? 中获取了代码。请喜欢这个答案)

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 2016-11-07
    • 2018-07-03
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多