Javascript document.URL 和 location.href 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5388036/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:58:30  来源:igfitidea点击:

Difference between document.URL and location.href

javascripturldomlocation

提问by eddie yang

I know that document.URLcan not be set, while location.hrefcan.

我知道document.URL不能设置,而location.href可以。

But the Document indicates:

但文件指出:

URL is a replacement for the DOM Level 0 location.hrefproperty.

URL 是 DOM Level 0location.href属性的替代品。

So when would we use document.URL?

那么我们document.URL什么时候使用?

回答by rsplak

You can getthe document.URL, but you can not setit. You can both get and set the location.href.

可以得到document.URL,但是不能设置。您可以同时获取和设置location.href.

In some webbrowsers, you are able to set the document.URLbut please don't, as it doesn't work in most browsers.

在某些网络浏览器中,您可以设置 ,document.URL但请不要,因为它在大多数浏览器中不起作用。

You gave the answer yourself!

你自己给出了答案!

var currentURL = document.URL;
alert(currentURL);

Learn more here

在此处了解更多信息

回答by Mark Kahn

They're interchangeable as far as getting data is concerned, but as you pointed out document.URL can not be set. I just always use location.href since it's a getter/setter.

就获取数据而言,它们是可以互换的,但是正如您指出的那样,无法设置 document.URL。我总是使用 location.href 因为它是一个 getter/setter。

回答by pop stack

Yes and no!

是与否!

alert(document.url);  
document.url="http://www.google.co.uk";  
alert(document.url);