最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect if URL() constructor is supported by browsers? - Stack Overflow

programmeradmin4浏览0评论

I want to detect if a browser support the URL() constructor.

I want to use it like this:

const url = new URL(urlString, [baseURLstring])

I can't find a proper method to check if it's supported by browser?

I want to detect if a browser support the URL() constructor.

I want to use it like this:

const url = new URL(urlString, [baseURLstring])

I can't find a proper method to check if it's supported by browser?

Share Improve this question edited Jun 5, 2017 at 6:46 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jun 5, 2017 at 6:44 David LIuDavid LIu 3792 gold badges4 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Assuming the check needs to be done in JavaScript -

Use if(typeof URL === "function")

If true URL is supported

Sample Code

if (typeof URL === "function") {
  const baseURLstring = "http://www.aaa.bbb/";
  let urlString = "/hello";
  const url = new URL(urlString, [baseURLstring]);
  console.log(url)
}
else if (navigator.userAgent.indexOf('MSIE') != -1 && typeof URL === 'object') {
  const baseURLstring = "http://www.aaa.bbb/";
  let urlString = "/hello";
  const url = new URL(urlString, [baseURLstring]);
  console.log(url)
}

  1. If const is supported, URL is likely also supported - except
  2. URL is not supported by IE at all it seems
    • https://developer.mozilla/en-US/docs/Web/API/URL/URL
    • http://caniuse./#feat=url
  3. if (window.URL) ...
发布评论

评论列表(0)

  1. 暂无评论