Surendra Sharma

Surendra Sharma

Search This Blog

Friday, March 6, 2015

How to access the current URL using JavaScript

Many times we need to access current URL of browser using Javascript. Suppose if there is URL like , now you can access this URL in different pieces as below

alert(window.location.protocol); // display "http:"
alert(; // display “"
alert(window.location.pathname); // display "guest/example.aspx"

You can get the complete and full URL by concatenating all these different parts

var currentURL = window.location.protocol + "//" + + "/" + window.location.pathname;

Please leave your comments or share this tip if it’s useful for you.

No comments:

Post a Comment