Javascript:设置 location.href 与位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2383401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Setting location.href versus location
提问by chimerical
When would you set locationto a URL string versus setting location.href?
什么时候设置location为 URL 字符串而不是设置location.href?
location = "http://www.stackoverflow.com";
vs
对比
location.href = "http://www.stackoverflow.com";
采纳答案by bobince
You might set locationdirectly because it's slightly shorter. If you're trying to be terse, you can usually omit the window.too.
您可以location直接设置,因为它稍短。如果你想简洁,你通常也可以省略window.。
URL assignments to both location.hrefand locationare defined to work in JavaScript 1.0, back in Netscape 2, and have been implemented in every browser since. So take your pick and use whichever you find clearest.
URL分配到两个location.href及location被定义为工作在JavaScript 1.0,早在网景2,并已在因为每一个浏览器来实现。因此,请选择并使用您认为最清楚的那个。
回答by psychotik
Even if both work, I would use the latter.
locationis an object, and assigning a string to an object doesn't bode well for readability or maintenance.
即使两者都有效,我也会使用后者。
location是一个对象,并且将字符串分配给对象对于可读性或维护来说并不是好兆头。
回答by Kirby L. Wallace
Like as has been said already, . locationis an objectBut that person suggested using either.But, you will do better to use the .hrefversion.
就像已经说过的那样,。location是一个对象但那个人建议使用两者之一。但是,使用该.href版本会更好。
Objects have default properties which, if nothing else is specified, they are assumed. In the case of the locationobject, it has a property called .href. And by not specifying ANY property during the assignment, it will assume "href" by default.
对象具有默认属性,如果没有指定其他内容,则假定它们。就location对象而言,它有一个名为 的属性.href。并且通过在赋值期间不指定任何属性,默认情况下它将假定为“href”。
This is all well and fine until a later object model version changes and there either is no longer a default property, or the default property is changed. Then your program breaks unexpectedly.
这一切都很好,直到以后的对象模型版本更改并且不再有默认属性,或者更改了默认属性。然后你的程序意外中断。
If you mean href, you should specify href.
如果您的意思是href,您应该指定href.
回答by Dovev Hefetz
A couple of years ago, locationdid not work for me in IE and location.hrefdid (and both worked in other browsers). Since then I have always just used location.hrefand never had trouble again. I can't remember which version of IE that was.
几年前,location在 IE 中对我不起作用,并且location.href在(并且都在其他浏览器中工作)。从那以后,我一直只是使用location.href,再也没有遇到过麻烦。我不记得那是哪个版本的 IE。
回答by Chadams
Just to clarify, you can't do location.split('#'), locationis an object, not a string. But you can do location.href.split('#');because location.hrefis a string.
只是为了澄清,你不能做location.split('#'),location是一个对象,而不是一个字符串。但是你可以这样做,location.href.split('#');因为它location.href是一个字符串。
回答by Gras Double
One difference to keep in mind, though.
不过,要记住一个区别。
Let's say you want to build some URL using the current URL. The following code will in fact redirect you, because it's not calling String.replacebut Location.replace:
假设您想使用当前 URL 构建一些 URL。下面的代码实际上会重定向你,因为它不是调用String.replace而是Location.replace:
nextUrl = window.location.replace('/step1', '/step2');
The following codes work:
以下代码有效:
// cast to string
nextUrl = (window.location+'').replace('/step1', '/step2');
// href property
nextUrl = window.location.href.replace('/step1', '/step2');
回答by BluE
With TypeScript use window.location.hrefas window.locationis technically an object containing:
在 TypeScript 中,window.location.href按原样使用window.location技术上是一个包含以下内容的对象:
Properties
hash
host
hostname
href <--- you need this
pathname (relative to the host)
port
protocol
search
Setting window.locationwill produce a type error, while
window.location.hrefis of type string.
设置window.location会产生类型错误,而
window.location.href类型是字符串。

