The window.location is an object. But when you execute location.toString()
it converts the object to the equivalent to location.href
.
My question is how? And can I set up objects to a similar behaviour?
location and window. location. href behave differently because the former is an object and the latter is a string. If you run string functions like indexOf() or toLowerCase() , you have to use window.
The href property of the Location interface is a stringifier that returns a string containing the whole URL, and allows the href to be updated.
The location. href property sets or returns the entire URL of the current page.
location. reload(true); reloads the page from the server instead of from the cache, window. location. reload(); would do the same thing as location.
You can add a toString
method to your object that returns what you want. In that case href
eg:
var obj = {
href:'',
toString:function(){
return this.href;
}
};
obj.href = 'http://stackoverflow.com';
obj.toString();
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