👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
There are several ways to redirect a web page: client side using meta tag in HTML and server side using HTTP redirect methods to name a few. In this article, we take a look at how to redirect a web page using JavaScript.
There are two possible ways to use JavaScript code for page redirection. Both involve the window location object.
href
The first method involves the href property on the location object (which, in turn, is part of the window object). The only thing you need to do is handle the redirect by assigning the URL you want the redirect to go to as location.href. You do this in a JavaScript function that will be executed when the page loads:
I added a script tag at the bottom of the document, just before the body tag. This is where our JavaScript goes. A function, called handleRedirect, has a parameter called url. We assign location.href to the value of this URL.
At the top of the document body, we have an onload event. This onload event executes the JavaScript function that we assign to it. Here we have passed the URL we would like it to go to. The redirect happens immediately and goes to the HTML portion of the Python.Engineering blog posts.