浏览器是否是IE判断以及当前js的来源地址获取

1 背景

  • 在引入第三方的库时需要用到对应的资源host地址
  • IE目前Cesium已经抛弃,但有时候项目还要支持,故需要判断增加1.83版本的支持

2 方案

2.1 当前js来源获取

var currentScript =
  document.currentScript ||
  (function () {
    
    
    var scripts = document.getElementsByTagName("script");
    return scripts[scripts.length - 1];
  })();

var src = currentScript.src;

2.2 IE浏览器判断

var theNavigator;
if (typeof navigator !== "undefined") {
    
    
  theNavigator = navigator;
} else {
    
    
  theNavigator = {
    
    };
}
var isInternetExplorerResult = false;
function isInternetExplorer() {
    
    
  var fields;
  if (theNavigator.appName === "Microsoft Internet Explorer") {
    
    
    fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(theNavigator.userAgent);
    if (fields !== null) {
    
    
      isInternetExplorerResult = true;
    }
  } else if (theNavigator.appName === "Netscape") {
    
    
    fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(
      theNavigator.userAgent
    );
    if (fields !== null) {
    
    
      isInternetExplorerResult = true;
    }
  }
  console.log("isInternetExplorerResult", isInternetExplorerResult);
  return isInternetExplorerResult;
}
isInternetExplorer();

Guess you like

Origin blog.csdn.net/Tmraz/article/details/120869468
Recommended