How to get current URL in jQuery?


For getting the current URL you can use attr() method or window.location.

Example

<html>
<head>
<title>url</title>
   <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
   <script type="text/javascript">
      $(document).ready(function () {
         $("button").click(function () {
            alert($(location).attr('href'));
            alert(window.location);
         });
      });
   </script>
</head>
<body>
<div>
<button type="button" id="btn">Display URL</button>
</div>
</body>
</html>

Updated on: 22-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements