ブラウザ・OS判定.js
2024-07-29
やっぱどうしてもブラウザ別の対応が必要になる(だいたいSafariのせい) code:script.js
/**
* ブラウザ名を取得する
* @returns {String} ブラウザ名
*/
window.navigator.getBrowser = () => {
const userAgent = window.navigator.userAgent.toLowerCase();
if (userAgent.includes("msie")) return "Internet Explorer";
if (userAgent.includes("trident")) return "Internet Explorer";
if (userAgent.includes("edge")) return "Microsoft Edge";
if (userAgent.includes("chrome")) return "Google Chrome";
if (userAgent.includes("safari")) return "Safari";
if (userAgent.includes("firefox")) return "Mozilla Firefox";
if (userAgent.includes("opera")) return "Opera";
return "Unknown";
};
/*
* OS名を取得する
* @returns {String} OS名
*/
window.navigator.getOS = () => {
const userAgent = window.navigator.userAgent.toLowerCase();
if (userAgent.includes("windows nt")) return "Microsoft Windows";
if (userAgent.includes("android")) return "Android";
if (userAgent.includes("iphone")) return "iOS";
if (userAgent.includes("ipad")) return "iPadOS";
if (userAgent.includes("mac os x")) return "macOS";
return "Unknown";
};