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 badges2 Answers
Reset to default 7Assuming 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)
}
- If
const
is supported, URL is likely also supported - except - URL is not supported by IE at all it seems
- https://developer.mozilla/en-US/docs/Web/API/URL/URL
- http://caniuse./#feat=url
if (window.URL) ...