Javascript Redirect

| | | | | | | | | | | | | |

👻 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.

If you want to add a pause before the page is redirected, you can do this using the setTimeout function. Here is how to do it:;

Everything else remains the same.

This method adds the source page to the stack history. It is accessible via the back button of the browser. another method to replace the url if you prefer to do that instead - we’ll see that later!

replace

Unlike the method we created above which assigns a new URL to a property on the location object, location.replace () is a method that assigns and replaces the original URL with whatever you have specific.

This method does not put the original URL on the browser stack, it replaces the target with the redirected URL. Here’s how:

As you can see, we didn’t have to all need the script tag for this one-liner. When the document is uploaded, it executes the location.replace method, which directs us to the site in parentheses.

Here ! You can now redirect to a new web page using JavaScript.

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Javascript Redirect, check other __main__ Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Julia Emmerson

Paris | 2023-04-01

I was preparing for my coding interview, thanks for clarifying this - Javascript Redirect in Python is not the simplest one. Will get back tomorrow with feedback

Xu Richtgofen

New York | 2023-04-01

Maybe there are another answers? What Javascript Redirect exactly means?. I just hope that will not emerge anymore

Julia Lehnman

Warsaw | 2023-04-01

I was preparing for my coding interview, thanks for clarifying this - Javascript Redirect in Python is not the simplest one. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically