

In order to redirect the user to another website after a certain time passes, you can use the following code: JavaScript Redirect: Redirect the Page After a Certain Period Note that, with this type of redirection, the visitors will not see your web page at all and be redirected to the target URL instantly. Simply replace the example URL with the one you want to redirect to.
JAVASCRIPT RUN AS SOON AS ELEMENT RENDERS CODE
js file, put the following code into that file and remember to link to it from the head of your page. In order to redirect the user to another website immediately after your website is opened, you can use the following code at the top of your page, within the element. JavaScript Redirect: Redirect the Page on Page Load You can also check this page to read more about how window.location works. Simply insert your target URL that you want to redirect to in the above code. It is better to know your alternatives but if you want a cross-browser compliant JavaScript redirect script, our recommendation will be to use the following in your projects: = "" Another point to keep in mind is that location.replace() replaces the current document by moving it from the history, hence making it unavailable via the Back button of the browser. For example, if you use top.location redirect within an iframe, it will force the main window to be redirected. Though the above lines of JS code accomplish a similar job in terms of redirection, they have slight differences in their usage. Sets the location of the topmost window of the current window. Sets the location of the current window itself. Replaces the location of the current window with the new one. Assigns a new URL to the current window. Sets the new href (URL) for the current window. The following is a list of possible ways that can be used as a JavaScript redirect: // Sets the new location of the current window. In JavaScript, window.location or simply location object is used to get information about the location of the current web page (document) and also to modify it. We will quickly list them and conclude with the recommended one. You can redirect a web page via JavaScript using a number of methods. Keeping the above points in mind, we can now continue with discussing about how JavaScript redirects work. Having said that, there are also some cases where a redirect can be quite useful based on the context of your web page or a specific feature of your web application and we will leave the decision whether to use a redirect or not, to you. Your site may even get removed from their index if you have redirects on your website that try to trick the search engine bots or that result in a bad user experience. Also, if you take the users to an irrelevant page after a timer, after they click on a button or image, or after they complete a certain action on your site, that will most probably result in the user wanting to leave your site at once and never come back.Īnother reason you should avoid redirects where possible is that the search engines do not favor websites that use redirects, especially if they are deceptive. For example, if you redirect the users to another website as soon as they land on your site, that will obviously cause frustration for them. Interruptive or unexpected web page redirects are considered to be very annoying from the user perspective, since they negatively affect the overall user experience.

Before we move on to our examples though, let’s briefly mention about the importance of using such redirects responsibly and only when you really have to.

In this tutorial, we will demonstrate how you can easily redirect a page using JavaScript by giving examples for various scenarios. You can redirect a web page to another page in a number of ways including server-side redirects, HTML meta refresh redirects and JavaScript redirects.
