The current URL in jQuery can be obtained by using the ‘href’ property of the Location object which contains information about the current URL. The ‘href’ property returns a string with the full URL of the current page.
Syntax:
$(location).attr('href')
Example-1:
<!DOCTYPE html> <html> <head> <title>Get current URL using jQuery?</title> </head> <body> <h1 style="color: green">GeeksForGeeks</h1> <b>Get current URL using jQuery?</b> <p>Click on the button below to get the current page URL</p> <p> The current URL is: <span class="output"></span></p> <button id="btn">Get current </button> <script src= </script> <script> $('#btn').click(function() { currLoc = $(location).attr('href'); document.querySelector('.output').textContent = currLoc; }); </script> </body> </html> |
chevron_right
filter_none
Output:
Before clicking the button:

After clicking the button:

Example-2:
<!DOCTYPE html> <html> <head> <title>Get current URL using jQuery?</title> </head> <body> <h1 style="color: green">GeeksForGeeks</h1> <b>Get current URL using jQuery?</b> <span class="output"></span> <script src= </script> <script> alert("host = " + $(location).attr('host') + "\nhostname = " + $(location).attr('hostname') + "\npathname = " + $(location).attr('pathname') + "\nhref = " + $(location).attr('href') + "\nport = " + $(location).attr('port') + "\nprotocol = " + $(location).attr('protocol')); </script> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- How to get the current URL using JavaScript ?
- How to get the size of screen, current web page and browser window using jQuery?
- How to get title of current HTML page using jQuery ?
- Convert relative path URL to absolute path URL using JavaScript
- How to get the first and last date of current month using JavaScript ?
- How to get the path of current script using Node.js ?
- Get current timestamp using Python
- How to get the current year using JavaScript ?
- Program to get the subdomain of a URL using PHP
- How to get protocol, domain and port from URL using JavaScript ?
- Get the YouTube video ID from a URL using JavaScript
- How to get URL Parameters using JavaScript ?
- How to get the file name from page URL using JavaScript ?
- How to get the new image URL after refreshing the image using JavaScript ?
- How to get current function name in PHP?
- How to get current formatted date dd/mm/yyyy in JavaScript ?
- How to get the current date in JavaScript ?
- How to get the current date and time in seconds?
- How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
- How to Get the Current Working Directory in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

