1

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.

  • closely related to http://stackoverflow.com/questions/3332756/difference-between-window-location-href-and-top-location-href. – Praveen Sep 27 '13 at 08:10

1 Answers1

3

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, ===.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • +1 I Objects can't be compared in JS. Thanks for letting us to know. – Praveen Sep 27 '13 at 08:13
  • Yeah. So, is it that objects in javascript cannot be compared? Since, frame busting code uses `top.location`!=`self.location` and it works correctly. – user2546340 Sep 27 '13 at 08:19
  • I doubt it, they must be doing something else, although `top.location!=self.location` always equals `true` meaning the frame will always "bust" – Bojangles Sep 27 '13 at 08:22
  • Isn't there any method to achieve `top.location==self.location`? – user2546340 Sep 27 '13 at 08:26
  • Objects CAN be compared using == (property by property) or === (by reference). – Virus721 Sep 27 '13 at 08:32
  • @user2546340 I think this can help you http://stackoverflow.com/questions/1068834/object-comparison-in-javascript – Praveen Sep 27 '13 at 08:34
  • @Virus721 What do you mean by reference? I've _never_ been able to compare objects without property by property iteration in JS – Bojangles Sep 27 '13 at 08:43
  • @Bojangles You can check if two references point to the same object using ===. At least this is what i read somewhere on SO some time ago. – Virus721 Sep 27 '13 at 08:58
  • So? How does that help in this case? – Bojangles Sep 27 '13 at 09:12
  • @Virus721 Do you mean "address"? How do we get reference or property of a object? – user2546340 Sep 27 '13 at 09:18
  • @user2546340 Non scalar variables (i.e objects and arrays) are references. – Virus721 Sep 27 '13 at 09:26
  • How do you compare objects using property or reference? Can you give one example? – user2546340 Sep 27 '13 at 09:30
  • Also, the location.assign() function assigns the value of type "String". Can we assign it in such a way that the value has a type "object" instead of "String"? – user2546340 Sep 27 '13 at 09:44
  • @Virus I said objects can't be _directly_ compared using `==` as the OP was doing, and it doesn't help in this case to see whether two references to an object point to the same real object. What have I said that's incorrect? So far, you appear to be the one who is incorrect in implying that objects can be directly compared with `==` – Bojangles Sep 27 '13 at 10:34
  • @Bojangles No you said they can't be compared using == or ===, which is a big difference. – Virus721 Sep 27 '13 at 12:00