Some of this information includes the current URL that the user is on.
Having the URL is useful as you can perform any number of actions based on the URL.
The easiest way to get the current URL is to use the
const url = window.location.href;
This works because the
window.location object includes a bunch of methods related to the current URL.
Here's a list of properties and what they return:
window.location.href: Returns the current URL.
window.location.host: Returns the hostname of the current URL.
window.location.hostname: Returns the hostname of the current URL.
window.location.port: Returns the port of the current URL.
window.location.protocol: Returns the protocol of the current URL.
window.location.pathname: Returns the pathname of the current URL.
window.location.search: Returns the search string of the current URL.
window.location.hash: Returns the hash of the current URL.
Use this code below to try them all:
Simply use the
window.location.href property to get the current URL, or use the
window.location object to get a specific piece of information.
Thanks for reading!
- How to Install Node on Windows, macOS and Linux
- Getting Started with Solid
- Getting Started with Express
- Create an RSS Reader in Node
- How to Serve Static Files with Nginx and Docker
- How to Set Up Cron Jobs in Linux
- How to deploy a MySQL Server using Docker
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting Started with Moment.js
- Using Push.js to Display Web Browser Notifications
- Setting Up Stylus CSS Preprocessor
- Setting Up a Local Web Server using Node.js