document.URL和location.href之间的区别

时间:2011-03-22 07:29:42

标签: javascript url dom location

我知道无法设置document.URL,而location.href可以设置。

但文件表明:

  

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

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

3 个答案:

答案 0 :(得分:51)

您可以获取 document.URL,但无法设置。 您可以获取并设置location.href

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

你自己给出了答案!

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

Learn more here

答案 1 :(得分:6)

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

答案 2 :(得分:-8)

是的,不!

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