My Blog

contains PHP and other web related content. (Sometimes there are some off topic things - don't freak out!)

document.URL vs document.location.href

When reviewing some javascript security ideas, I came across the document.URL property. Turns out that my normal way of retrieving the location (document.location.href) is both a getter and a setter. The document.URL is just a getter.

Check it out with this code:

1
2
3
4
alert(document.URL);
alert(document.location.href);
document.URL = 'http://google.com';
document.location.href = 'http://yahoo.com';

The results are simple: you will get the current location twice – and then an error. If you comment out the document.URL line, it will redirect to yahoo.

Tags:

One Response to “document.URL vs document.location.href”

  1. walkingp says:

    but in ie,the document.URL still take effect.switch the last two statement,you’ll find out that it will redirect to google.com.

Leave a Reply

  • twitter loader

Follow me on twitter: @aaronsaray

The views on this website are my own and do not reflect the opinions of my employer or clients.
Creative Commons License Home | Open Source | Book | Music | Art | Bio | Resume | Contact
My Baby