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?
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)
}
const
is supported, URL is likely also supported - exceptif (window.URL) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With