Difference between document.URL and location.href

前端 未结 3 1772
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 08:45

I know that document.URL can not be set, while location.href can.

But the Document indicates:

URL is a replacement f

相关标签:
3条回答
  • 2020-12-13 08:56

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

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

    You gave the answer yourself!

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

    Learn more here

    0 讨论(0)
  • 2020-12-13 09:04

    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.

    0 讨论(0)
  • 2020-12-13 09:04

    Yes and no!

    alert(document.url);  
    document.url="http://www.google.co.uk";  
    alert(document.url);  
    
    0 讨论(0)
提交回复
热议问题