简体   繁体   English

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

[英]Alert when page opened not in Chrome

I'm trying to get an alert to pop up when the user opens my webpage in a browser other than Chrome. 当用户在除Chrome浏览器之外的浏览器中打开我的网页时,我正试图弹出警报。 I have this: 我有这个:

if (/ what to put here to make the below show only if the browser is not Google Chrome? /) { alert( "Please use Google Chrome to access this site.\\nSome key features do not work in browsers other than Chrome." ); 如果(// 仅当浏览器不是Google Chrome时在此处显示以下内容? /){alert(“请使用Google Chrome浏览器访问此网站。\\ n某些关键功能在Chrome浏览器以外的浏览器中不起作用。” ); } }

Found this here as a possible condition: navigator.userAgent.search("Chrome"), but can't make it work. 在此处发现这种情况的可能原因:navigator.userAgent.search(“ Chrome”),但无法使其正常工作。 Please advise. 请指教。 :-) :-)

This should to it. 这应该。

 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."); } 

As the comments above point out by referencing questions you can test for chrome in the userAgent. 正如上面的注释通过引用问题指出的那样,您可以在userAgent中测试chrome。 But you would want to reverse that: 但是您想扭转这种情况:

  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) 

This will solve the problem unless the user is spoofing their userAgent. 除非用户欺骗其userAgent,否则这将解决问题。 This is unusual though. 这是不寻常的。 To fully check you should also check for a feature chrome has AND the 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