Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does location.toString() report the same as location.href?

Tags:

javascript

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?

like image 517
Dean James Avatar asked Jun 13 '11 10:06

Dean James


People also ask

What is the difference between window location and location href?

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.

Does window location href return a string?

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.

What does document location href return?

The location. href property sets or returns the entire URL of the current page.

What is the difference between location reload () and window location reload ()?

location. reload(true); reloads the page from the server instead of from the cache, window. location. reload(); would do the same thing as location.


1 Answers

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();
like image 105
Mic Avatar answered Oct 23 '22 03:10

Mic