How can one detect if a given browser has the searchParams
prototype for URL
? https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams states that Chrome and FF do but Edge does not, but I wish to detect using JavaScript.
I've messed around with isPrototypeOf
, but don't think it is applicable.
Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.
The Object. getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
To check if an object has a method in TypeScript: Mark the specific method as an optional key in the object's type. Use typeof to check if accessing the method on the object returns a value with a function type. If the typeof operator returns true , the method exists.
The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.
In supporting browsers, there will be an URLSearchParams
constructor available on global object, so like any other global Constructor,
'URLSearchParams' in window
or
typeof window.URLSearchParams === 'function'
and alike will do.
const support = typeof window.URLSearchParams === 'function';
console.log('supports URLSearchParams API:', support);
var url = new URL('https://stackoverflow.com/questions/47824782/how-to-tell-if-an-object-has-a-given-prototype?support="true"');
if(support){
console.log(url.searchParams.get('support'));
}
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