How to Get the Current Page URL with JQuery
Getting the Current page URL with JQuery is very much important, especially when redirecting or when tracking users’ activities.
There are two methods you can use to get the current page URL with jquery:
- Use $(location).attr(“href”)
- Use window.location.href
Use $(location).attr(“href”) to get the current page URL
You can use the location object and attr method to get the current page URL as shown in the example below.
$(function(){
var url = $(location).attr("href");
alert(url);
});
Use window.location.href to get the current page URL
You can also use the location and href objects to get the current page URL as shown in the example below