JavaScript provides multiple ways to get the current URL of a webpage. In this article, we will discuss the most common methods to achieve this.
Page Details
Viewport Width
There are two methods to get the viewport width: window.innerWidth and document.documentElement.clientWidth. The former is more accurate. The latter has better browser support. To get the best of both worlds, try innerWidth first, and fallback to clientWidth if not supported. Source https://vanillajstoolkit.com/reference/viewport/viewport-width/
Get the height of the viewport
There are two methods to get the viewport height: window.innerHeight and document.documentElement.clientHeight. The former is more accurate. The latter has better browser support. To get the best of both worlds, try innerHeight first, and fallback to clientHeight if not supported. Source https://vanillajstoolkit.com/reference/viewport/viewport-height/
Notify when element size is changed
Source https://github.com/roeib/JavaScript-snippets#notify-when-element-size-is-changed
Check if a node is in the viewport
Source https://github.com/roeib/JavaScript-snippets#check-if-a-node-is-in-the-viewport
Find the distance from a given element to the top of the document
Use getVerticalOffset to find the distance from a given element to the top of the document.
Return the current URL
Use Window.location.href to get the URL of the current page.
Redirection: How to Change the URL in JavaScript
We’ll go through the different built-in methods provided by JavaScript to implement URL redirection. In fact, JavaScript provides the location object, a part of the window object, which allows you to perform different URL-related operations.
Calculate the size of scrollbar
The clientWidth property indicates the width without scrollbar. The offsetWidth, on the other hand, includes the scrollbar if there is.
Check If the Document is Ready
To check if the document is ready and execute some code, you bind an event listener to the DOMContentLoaded event of the document object.