Get URL without its query string (JavaScript)
Getting the URL of the current page without its query parameters:
HTML code:
<div class="example"></div>
JavaScript code:
<script>
// Getting the URL of the current page without its query string
var url = location.protocol + '//' + location.hostname + location.pathname;
// Printing the result
document.querySelector('.example').textContent = url;
</script>
Links
- How to get the URL without any parameters in JavaScript edureka.co/community/87878/how-to-get-the-url-without-any-parameters-in-javascript
- location.host vs location.hostname and cross-browser compatibility? stackoverflow.com/questions/6725890/location-host-vs-location-hostname-and-cross-browser-compatibility
- Location https://developer.mozilla.org/en-US/docs/Web/API/Location
- Location: pathname property developer.mozilla.org/en-US/docs/Web/API/Location/pathname
Browser support
- Windows
- Edge 12.0+
- Firefox 53.0+
- Google Chrome 1.0+
- Opera 12.1+
- Linux
- Firefox 53.0+
- Google Chrome 1.0+
- Opera 12.1+
- iOS
- Safari 1.0+
- Android
- Samsung Internet 1.0+
- Chrome 18.0+
- Firefox 53.0+
- Opera 12.1+