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>

Advertisements