Question

I want to know how "location"(window.location) object is compared in javascript i.e. which values are taken into consideration while comparing two "location" objects.

Suppose i have top.location = "http://www.abc.com" and self.location = "http://www.abc.com". If i compare them as (top.location == self.location), it gives false. Whereas, if i compare them as (top.location.href == self.location.href), it will gives true.

Can anyone explain why this happens?

Thanks in advance.

Was it helpful?

Solution

top.location and self.location are Location objects. Objects in JavaScript can't be directly compared using == or ===, which is why top.location != self.location.

Since top.location.href and self.location.href are conventional strings, they can be compared as usual using == or, better, ===.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top