How to get the current page url using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the syntax $(location).attr("href");

The following example will show you how to find the current page URL using jQuery.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>jQuery Get Current Page URL</title>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         $("button").click(function(){
  •             var pageURL = $(location).attr("href");
  •             alert(pageURL);
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <button type="button">Get URL</button>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.