JavaScript Redirect

A URL redirect or URL redirection usually refers to when a web page, as soon as it is loaded, redirects to another web page. This technique can be useful if the content on your page has moved to another page.

In JavaScript, you script a redirect using the same code you use to load a page.

Redirect code

To create a JavaScript redirect, place the following code between the <head> tags of your HTML page (replacing the URL with the URL that you want to redirect to).

Redirect with onclick

You can use the same code to create a JavaScript hyperlink, so that when the user clicks on something, they are redirected to the new URL. To do this, use the onclick event handler to initate the redirect (more event handlers).

Redirect Function

Even better, you can place your redirect code into a JavaScript function. Then you can call that function from within your HTML code. Like this:

In the above examples, the page will only redirect if the user has JavaScript enabled on their browser. If you want the redirection to occur regardless, try using an HTML redirect.