Redirect to another page on load

To execute a JavaScript-based web page redirection, multiple methods are available. For optimal cross-browser compatibility, it is advisable to utilize the following JavaScript redirect scripts. These scripts ensure consistent behavior across different browsers while seamlessly guiding users to the desired destinations.

window.location.href = url;
example
window.location.href = "https://net-informations.com";

You can use window.location object to load another page in JavaScript.

example
window.location = "https://net-informations.com";

Difference between window.location and location.href

  1. window.location is an object that holds all the information about the current document location (host, href, port, protocol etc.).

  2. location.href is shorthand for window.location.href (you call location from global object - window, so this is window.location.href), and this is only a string with the full URL of the current website.

Other methods of JavaScript redirection

Assigns a new URL to the current window.

window.location.assign("https://net-informations.com");

Replaces the location of the current window with the new one.

window.location.replace("https://net-informations.com");

Sets the location of the current window itself.

self.location = "https://net-informations.com";

Sets the location of the topmost window of the current window.

top.location = "https://net-informations.com";

Conclusion

To redirect users to another page upon page load using JavaScript, the window.location object can be employed. By setting its href property to the desired URL, the user is automatically redirected to the designated page as soon as the current page loads. This technique is useful for seamlessly guiding users to different content or destinations when they access a web page.