Get the current URL in JavaScript
Getting the full URL of the current page in Vanilla JavaScript:
HTML code:
<div class="example">
</div>
JavaScript code:
<script>
// Getting the full URL of the current page
var currentURL = window.location.href;
// Printing the result
document.querySelector('.example').textContent = currentURL;
</script>
Links
- location: href property: developer.mozilla.org/en-US/docs/Web/API/Location/href
- JavaScript Window Location: www.w3schools.com/js/js_window_location.asp
Browser support
- Windows
- Internet Explorer 4.0+
- Edge 12.0+
- Firefox 1.0+
- Google Chrome 1.0+
- Opera 3.0+
- Safari 3.1+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 2.0+
- Linux
- Firefox 1.0+
- Google Chrome / Chromium
- Opera 5.0+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 2.0+
- Hv3
- iOS
- Safari 1.0+
- Android
- Samsung Internet 1.0+
- Chrome 18.0+
- Firefox 4.0+
- Opera 12.1+
More
- Adding a class to all sibling elements of a specific type in JavaScript
- Array to string in JavaScript
- Calculate sum of HTML table column in JavaScript
- Detecting a newly generated element and adding a class to it in JavaScript
- Generating an HTML list out of page headings in JavaScript
- Getting the last class name in classList collection
- Removing the last character from a string in Javascript
- Transliterating Russian to English in JavaScript