簡體   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