Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / Javascript
Tip/Trick

Get current page URL using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
24 Nov 2012CPOL 61.5K   6   4
Get current page URL on client side.

Introduction 

In our web application sometimes we require current page URLl on client side, like when we share any script or perform any action according to the page name. 

Using the code

Here I am sharing the demo code to get the current page URL using JavaScript:

JavaScript
//  Get current page url using JavaScript
var currentPageUrl = "";
if (typeof this.href === "undefined") {
    currentPageUrl = document.location.toString().toLowerCase();
}
else {
    currentPageUrl = this.href.toString().toLowerCase();
}

Points of Interest

The above code works perfectly with popups also. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Hi, I am Samrat Banerjee from India.
I am a Software Engineer working in .net platform.
I love to explore new technologies.

Comments and Discussions

 
GeneralMy vote of 1 Pin
pepri26-Nov-12 12:14
pepri26-Nov-12 12:14 
GeneralWonderful Pin
Rahul Khadikar25-Nov-12 21:08
Rahul Khadikar25-Nov-12 21:08 
GeneralMy vote of 5 Pin
vikram190725-Nov-12 17:56
vikram190725-Nov-12 17:56 
Thanks samrat. nice artical Smile | :)
GeneralMy vote of 5 Pin
Júnior Pacheco24-Nov-12 5:47
professionalJúnior Pacheco24-Nov-12 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.