Q:

How to Get the Current URL with JavaScript

0

How to Get the Current URL with JavaScript

All Answers

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

Use the window.location.href Property

You can use the JavaScript window.location.href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

The following example will display the current url of the page on click of the button.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Current URL in JavaScript</title>
</head>
<body>
    <script>
    function getURL() {
        alert("The URL of this page is: " + window.location.href);
    }
    </script>
     
    <button type="button" onclick="getURL();">Get Page 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 Include a JavaScript File in another JavaSc... >>
<< How to Return Multiple Values from a Function in J...