How to Obtain the URL of a Web Page using Javascript


Javascript



In this article, we will show how to obtain the URL of a web page using Javascript.

The code to obtain the URL of the current web page that a user is on is:

To display it out to the output screen, we would use the document.write() function, using the following line:





So, if we use the above line with this current page, we will get the following output:



As you can see, the line directly above should match the URL that you see in your browser's search box.

And this is how quite simply we can obtain the URL of the current web page using Javascript.

HTML Comment Box is loading comments...