0

Which of the following should I use to assign the location object to a local variable in javascript?

var l = location;
var l = window.location;
var l = window.document.location;

Why are there so many methods? Is there any difference?

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • 2
    @Xander the duplicate has a comment referring to a better duplicate, check it out http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip/2431375#2431375 – Esailija May 30 '12 at 22:07
  • @Xander Thanks! That answer is helpful. I voted to close my own question. :) – Web_Designer Jun 05 '12 at 16:11

4 Answers4

3

I would use window.location, to help ensure I was really talking about the global object's property, and not some local variable which happens to be called location.

The W3C specs say:

The location attribute of the Window interface must return the Location object for that Window object's Document.

so those two are indeed equivalent (in fact exactly the same object).

location just happens to be a short hand for window.location, so long as there's no other variable named location in the scope.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • what if `window` is some local variable? – Esailija May 30 '12 at 21:52
  • 1
    @Esailija that's indeed possible, as I'm sure you know, but you can't insure against stupidity... That's why I said _help_ ensure. – Alnitak May 30 '12 at 21:52
  • 1
    Yes you can, `new Function( "return this;")().location`, anything less and you might as well use `location` directly :P – Esailija May 30 '12 at 21:55
  • the w3c spec speaks there about the window.location attribute. not the document.location attribute. big differences and problems can occur if you assume they are the same. – Tschallacka May 30 '12 at 21:55
  • @MichaelDibbets yeah, I'm trying to understand thay myself - since `document` **is** `window.document` it's unclear exactly what the distinction is. – Alnitak May 30 '12 at 21:57
  • Window.document refers to the first loaded document. basically the url you typed. however document.location can refer to a whole different url if your page has been framed. why this is? by window.document.location you go down a specific ladder. by taking document.location you dont care where on the ladder you are. – Tschallacka May 30 '12 at 22:03
  • 2
    @MichaelDibbets I think you're confusing `window.document.location` with `parent.document.location` in the iframe case. – Alnitak May 30 '12 at 22:07
  • @Esailija `(1,eval)("this").location` works too and is shorter. – chuckj May 31 '12 at 01:00
3

They all refer to the same object. Though, window.location is more explicit, cross browser compliant and can prevent collisions with other variables named location in different scopes.

Why so many ways?

Browser differences/vestigial remnants from a bygone era.

Alex
  • 34,899
  • 5
  • 77
  • 90
0

In a web browser, window is always at the top of the variable lookup chain. Thus, a global variable x is also window.x. For example:

<script>
var x = 1;
window.alert(window.x);
</script>

Will alert 1. Similarly, location will also resolve as window.location provided there is no more local variable called location in the chain.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 1
    @Xander - Correct, if `x` exists within the local scope of a function, it will not be a property of `window`. Thus, `window` is perhaps a better way to fully qualify a global version of `location` if there was also a local variable called `location`. Provided, of course, there's not *also* a local variable called `window` :) – Mike Christensen May 30 '12 at 22:02
-1

window.location is the _top location. document.location is the current html page location

if you are in an iframe document.location of the iframe will differ because window.location refers to the parent page.

by having a check on this you can break from frames by checking if the are te same. if not break from unauthorised framing. or check if my parent page for the i frame is the right one, if not redirect to the right one.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • I don't think so, provide a jsfiddle. I get `document.location === window.location` no matter what iframe I am in – Esailija May 30 '12 at 22:01
  • Its classic location checking. you might want to add .href to you location attribute. if(document.location.href != window.location.href) window.location.href=document.location.href;this code has worked for me since 1998 to prevent illegal framing. – Tschallacka May 30 '12 at 22:10
  • 1
    Since they are objects, and the equality test gives true, this means that they point to exactly the same object in memory. How could .href be different for them? – Esailija May 30 '12 at 22:11
  • 2
    @MichaelDibbets are you _sure_ you're not thinking of `parent.document.location` ? – Alnitak May 30 '12 at 22:13