【发布时间】:2019-06-10 11:30:49
【问题描述】:
我们制作了一个针对 Google Chrome/Safari 优化的网络应用。我愿意
- 强制所有安卓用户使用谷歌浏览器访问网站。如果 Chrome 不可用,我不希望他们访问该网站。
- 强制所有 iOS 用户使用 Safari
你能建议我正确的方法吗?
谢谢
【问题讨论】:
标签: android google-chrome safari user-agent
我们制作了一个针对 Google Chrome/Safari 优化的网络应用。我愿意
你能建议我正确的方法吗?
谢谢
【问题讨论】:
标签: android google-chrome safari user-agent
我想我有一个简单的解决方案给你。
首先你需要检查他们使用的是什么浏览器,然后如果他们没有使用你们优化的那两个浏览器之一,你需要将他们引导到 chrome 或 safari。
所以:
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
// Chrome 1 - 71
var isChrome = !!window.chrome && (!!window.chrome.webstore ||
!!window.chrome.runtime);
// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;
如果它们在该浏览器上,则上面的代码将分配布尔值 true,否则将分配 false。
现在你可以决定做什么,因为你知道他们使用的是什么浏览器
// If they are not on chrome and safari
if (!isChrome && !isSafari) {
// replace this with whatever you want to do
// in this case I make your website text red, but you could display a link
// to chrome app in playstore or whatever you need
document.getElementByTagName("body").style.color = "red";
}
【讨论】: