How to get the current URL using Jquery
In this tutorial, we are going to see how to get the current URL using Jquery. You can simply use the method attr() of jQuery to get the value of the attribute ‘href’. The following example will show you how to find the URL of the current page using jQuery.
How to get the current URL using Jquery
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>How to get the current URL</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ var url = $(location).attr("href"); alert(url); }); }); </script> </head> <body> <button type="button">Get URL</button> </body> </html>
Result |
---|