繁体   English   中英

当页面不在Chrome中打开时发出警报

[英]Alert when page opened not in Chrome

当用户在除Chrome浏览器之外的浏览器中打开我的网页时,我正试图弹出警报。 我有这个:

如果(// 仅当浏览器不是Google 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具有AND 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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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