How To Tell If An Object Has A Given Prototype?
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 Ed
Solution 1:
In supporting browsers, there will be an URLSearchParams
constructor available on global object, so like any other global Constructor,
'URLSearchParams'inwindow
or
typeofwindow.URLSearchParams === 'function'
and alike will do.
const support = typeofwindow.URLSearchParams === 'function';
console.log('supports URLSearchParams API:', support);
var url = newURL('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'));
}
Solution 2:
You may want to add more to the Constructor I created, but it's like:
/* just for testing */URL = undefined;
if(typeofURL === 'undefined'){
URL = function(url){
functionsp(){
var s = url.replace(/^.+?/, '').split('&'), o = {};
for(var i=0,p,l=s.length; i<l; i++){
p = s[i].split('='); o[p[0]] = p[1];
}
this.get = function(param){
if(typeof o[param] === 'undefined'){
returnfalse;
}
return o[param];
}
}
this.searchParams = new sp;
}
}
var url = newURL('http://subdomain.domain.org/somefolder/deeperfolder?param1=value1¶m2=value2¶m3=value3&test=cool');
var params = url.searchParams;
console.log(params.get('nope'));
console.log(params.get('param2'));
console.log(params.get('test'));
Post a Comment for "How To Tell If An Object Has A Given Prototype?"