Q:

How to get the current page url using jQuery

0

How to get the current page url using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

You can use the Location object and jQuery attr() method to get the URL of the current page.

Let's take a look at an example to understand how it actually works:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to calculate the number of words in a string u... >>
<< How to highlight alternate table row using jQuery...