Home > Article > Daily Programming > How to get the current page url with jQuery
jQuery gets the current page url, we can do it through the syntax $(location).attr("href").
Below we will combine a simple code example to introduce to you the implementation method of jQuery to obtain the current page url.
The code example is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jquery获取当前页面url示例</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(function(){ $("button").click(function(){ var pageURL = $(location).attr("href"); alert(pageURL); }); }); </script> </head> <body> <button type="button">获取当前地址</button> </body> </html>
In the above code, a button button is first defined. When the button is clicked, the click event is triggered. In this event, $(location).attr("href ") method to get the complete url address of the current page.
attr() method can set or return the attribute value of the selected element, here is the set href attribute. Finally, the location information obtained is displayed in the front desk through alert, which is the current page URL address.
Then the result of getting the url is as follows:
This article is about jQuery’s method of getting the URL of the current page. It is also very simple. I hope it will help Friends in need help!
The above is the detailed content of How to get the current page url with jQuery. For more information, please follow other related articles on the PHP Chinese website!