ADVERTISEMENT
get the current page URL with JQuery

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:

  1. Use $(location).attr(“href”)
  2. 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

<script>
    $(function(){
      var url =  window.location.href;  

      alert(url);
    });
  </script>