Home  >  Article  >  Daily Programming  >  How to get the current page url with jQuery

How to get the current page url with jQuery

藏色散人
藏色散人Original
2018-12-18 13:56:5213351browse


jQuery gets the current page url, we can do it through the syntax $(location).attr("href").

How to get the current page url with jQuery

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:

How to get the current page url with jQuery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn