How do i get the current URL in JavaScript?

The window.location object can be used to get the current page URL and also we can use to redirect the browser to a new page.

The window.location.href property returns the URL of the current page.
Eg:
<script>
Console.log("The curret page URL is: " + window.location.href);
</script>
window.location.href returns the href (URL) of the current page
window.location.hostname returns the domain name of the web host
window.location.pathname returns the path and filename of the current page
window.location.protocol returns the web protocol used (http: or https:)
window.location.assign loads a new document

No comments:

Post a Comment