views:

151

answers:

4

In Javascript, is there a case where self.location != document.location?

+1  A: 

I suppose it depends on the scope. As far as I know it is possible to (re)define self in an object or even in the global scope, so in that case self.location would point to nothing. Like this:

//[in global scope]
var self = new SomeObject;
alert(self.loction); //undefined

//in a constructor
function SomeObject(){
  var self = this;
  alert(self.location); //undefined
}

Bottom line seems: do not blindly depend on the availability of self as alias for document

KooiInc
+3  A: 

The window.self property is a reference back to window, and window.location is the same object as document.location.

So, the only possibility to get that expression to be true, is to redefine either self or document.

Guffa
and window is a property of the global object which is a reference back to the global object. The DOM is full of mysteries... :)
galambalazs
A: 

document.location is a string, not an object, and it has been replaced by document.URL.

An url that is redirected by the server does not have to update the window.location, but the document.URL always shows the path to the current document.

kennebec
A: 

Since I can't post a comment, apparently self.location == document.location == window.location in a frame. Only top.location is different. (Tested in Firefox 3.6.6 and Internet Explorer 8)

Hello71