Search code examples
javascriptencodingurl-encodingurldecode

how to get page url without decode in javascript?


How can I get page URL from window.location.href without decode in javascript?

For example we can not get exactly this URL: http://example.com/report#title=example_report&url=project_chart%2F%3Fproject_id%3D77.

When we use window.location.href in javascript , we will get this URL:

http://example.com/report#title=example_report&url=project_chart/?project_id=77.

But I want to get exactly the same real URL.

Any solution?

Edited

as @Eugenio told, $(document)[0].URL works fine , but Is it safe?!


Solution

  • Just to make a clear and concise answer I will sum up all the comments.

    For your problem the best solution is to use document[x].url where x is the index of the URL part that you want to use.

    The main difference for your problem between window.location.href and document.url is that the last one gives you the URL in a string format, whilest the other return the URL already parsed.

    Using either one is completely normal and safe and is widely adopted in all modern browsers.

    var url1 = document.URL;
    var url2 = window.location.href;
    
    document.getElementById("documentUrl").append (url1);
    document.getElementById("windowLocationUrl").append (url2);
    <div id="documentUrl">document.url: </div>
    <div id="windowLocationUrl">window.location.href: </div>

    There is no difference in this particular snippet example because there are no parameters attached to the URL. Anyway, hope this helped. Cheers!