用js判断不同浏览器
<script>
(function () {
// 检测浏览器类型
function detectBrowser() {
const userAgent = navigator.userAgent;
// 检查 Microsoft Edge(包含 'Edg' 且排除 Safari)
if (userAgent.indexOf('Edg') > -1 && userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1) {
alert('Microsoft Edge');
return 'Microsoft Edge';
}
// 检查 Chrome(只包含 'Chrome' 且没有 'Edg')
if (userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Edg') === -1) {
alert('Chrome');
return 'Chrome';
}
// 检查 Firefox
if (userAgent.indexOf('Firefox') > -1) {
alert('Mozilla Firefox');
return 'Mozilla Firefox';
}
// 其他浏览器
return 'Unknown Browser';
}
// 调用函数检测浏览器类型
const browser = detectBrowser();
// 输出浏览器类型
console.log(`当前浏览器是:${browser}`);
})();
</script>