当用户在Chrome以外的浏览器中打开我的网页时,我正在尝试设置提醒。我有这个:
if(/如果浏览器不是谷歌Chrome浏览器,请在此处播放以下内容?){alert(“请使用Google Chrome浏览器访问此网站。\ n某些主要功能在Chrome以外的浏览器中无效。” ); }
在这里发现这是一个可能的条件:navigator.userAgent.search(“Chrome”),但无法使其工作。请指教。 :-)
这应该是它。
var isChrome = !!window.chrome; // "!!" converts the object to a boolean value
console.log(isChrome); // Just to visualize what's happening
/** Example: User uses Firefox, therefore isChrome is false; alert get's triggered */
if (isChrome !== true) {
alert("Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome.");
}
正如上面的评论通过引用问题指出的那样,您可以在userAgent中测试chrome。但是你想反过来说:
let notChrome = !/Chrome/.test(navigator.userAgent)
let alertMessage = "Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome."
if(notChrome) alert(alertMessage)
除非用户欺骗他们的userAgent,否则这将解决问题。这不寻常。要完全检查您还应该检查功能chrome has和userAgent:
let notChrome = !/Chrome/.test(navigator.userAgent) && !("webkitAppearance" in document.body.style)
let alertMessage = "Please use Google Chrome to access this site.\nSome key features do not work in browsers other than Chrome."
if(notChrome) alert(alertMessage)