最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there any difference with using only location vs using window.location across browsers - Stack Overflow

programmeradmin3浏览0评论

I find myself always writing:

console.log(window.location.href);

without even thinking about it. The majority of answers on SO also write it this way. Is there any reason why I can't just write:

location.href

since location is an object at window level? Are there any cross-browser patibility issues with this?

To Clarify: I know there is document.location - that is NOT what this question is about. This is about if there is any difference with using only location vs using window.location across browsers.

I find myself always writing:

console.log(window.location.href);

without even thinking about it. The majority of answers on SO also write it this way. Is there any reason why I can't just write:

location.href

since location is an object at window level? Are there any cross-browser patibility issues with this?

To Clarify: I know there is document.location - that is NOT what this question is about. This is about if there is any difference with using only location vs using window.location across browsers.

Share Improve this question edited Mar 3, 2014 at 22:18 Robert Harvey 181k48 gold badges348 silver badges512 bronze badges asked Oct 17, 2013 at 9:56 CodingIntrigueCodingIntrigue 78.6k32 gold badges175 silver badges177 bronze badges 6
  • 4 Do you even read? It's not a duplicate of location vs location.href, but about x vs window.x – Tibos Commented Oct 17, 2013 at 10:00
  • 1 @NikhilAgrawal Nope. You need to read the question again. Or the ment above yours. Both will explain why it is explicitly not about an object vs a property, but the scope of a particular object. – CodingIntrigue Commented Jan 9, 2014 at 15:13
  • 1 People are getting way too close-happy. This question isn't even close to being related to the question it's supposedly a duplicate of. – Tmdean Commented Mar 1, 2014 at 23:48
  • @Tmdean: it's still a duplicate regardless, for example the first question in the "related" list. stackoverflow./questions/4709037/… – Qantas 94 Heavy Commented Mar 1, 2014 at 23:53
  • @Qantas94Heavy The question you linked to is not the first linked one in the related list. None of those in the related list have anything to do with my question. Yours is pletely valid but I searched for a long time and came up with nothing – CodingIntrigue Commented Mar 2, 2014 at 14:09
 |  Show 1 more ment

1 Answer 1

Reset to default 16

There are some differences.

In global scope, there is absolutely no difference between them, but in other cases you might get in trouble:

function () {
  var location = { 'href' : '123' } ;
  console.log(window.location.href) // actual url
  console.log(location.href) // '123'
}

This stems from the fact that if you write location without prefixing it with window, it will go up through every scope to find a variable named location. Eventually it will find it in window, unless another scope declared one as well. Obviously the reverse is true as well:

function () {
  var window = { 'location' : { 'href': '123' } };  
  console.log(window.location.href) // '123'
  console.log(location.href) // actual url
}

I for one prefer to prefix the global variables with window because that way i immediately know they are global and also because when i find a global variable that is not prefixed with window, i know it is a typo missing a var, but that is purely personal preference.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论