{"id":146,"date":"2019-09-16T16:55:25","date_gmt":"2019-09-16T16:55:25","guid":{"rendered":"https:\/\/tutorialio.com\/?p=146"},"modified":"2025-11-04T10:31:25","modified_gmt":"2025-11-04T10:31:25","slug":"redirect-to-another-webpage-in-javascript","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/redirect-to-another-webpage-in-javascript\/","title":{"rendered":"Redirect to Another Webpage in JavaScript"},"content":{"rendered":"<p>There are many situations where you might want to redirect users to a different page based on some action they took. For example, if the users deleted a post, you might want to redirect them to the homepage after deletion. In this tutorial, you will learn about different methods that can be used to redirect users to another page with pure JavaScript. You don\u2019t need to use jQuery for simply redirecting users to another webpage.<\/p>\n<h2 id=\"redirect-users-to-another-webpage-using-windowlocationhref\">Redirect Users to Another Webpage Using window.location.href<\/h2>\n<p>This is one of the most common techniques for redirecting users to another webpage in pure JavaScript. All you have to do is set the value of <code>window.location.href<\/code> to the destination URL and you will be good to go.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">window.location.href = 'destination_url';<\/code><\/pre>\n<p>There are a couple of things that you need to keep in mind. First, when specifying absolute URLs, you need to start the URL with <code>http:\/\/<\/code> or <code>https:\/\/<\/code>. You cannot drop the protocol for absolute URLs. Otherwise, the supplied value will be treated as a relative path. The following example will make things clear. We will work with the assumption that we are currently on <code>https:\/\/tutorialio.com\/javascript.php<\/code>.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">\/\/ This will redirect to https:\/\/tutorialio.com\nwindow.location.href = \"\/\";\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/google.com\nwindow.location.href = \"google.com\";\n\n\/\/ This will redirect to https:\/\/google.com\nwindow.location.href = \"https:\/\/google.com\";\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/php\/faqs\/\nwindow.location.href = \"php\/faqs\/\";<\/code><\/pre>\n<p>Let\u2019s see another example, where the current page is <code>https:\/\/tutorialio.com\/javascript\/faqs\/<\/code>.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">\/\/ This will redirect to https:\/\/tutorialio.com\nwindow.location.href = \"\/\";\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/google.com\nwindow.location.href = \"google.com\";\n\n\/\/ This will redirect to https:\/\/google.com\nwindow.location.href = \"https:\/\/google.com\";\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/php\/faqs\/\nwindow.location.href = \"php\/faqs\/\";\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/php\/faqs\/\nwindow.location.href = \"\/php\/faqs\/\";<\/code><\/pre>\n<p>When you are using <code>window.location.href<\/code>, you will be simulating a link click. This means that the current page will be stored in the session history. In other words, the users will be able to click on the back button to go to the previous page which redirected them.<\/p>\n<p>This can be troublesome in situations where the redirect happened without any user interactions. In such cases, the users will be stuck in a never ending loop if they keep clicking on the back button.<\/p>\n<h2 id=\"redirect-users-to-another-webpage-using-windowlocationreplace\">Redirect Users to Another Webpage Using window.location.replace()<\/h2>\n<p>If you want to redirect your users to another webpage in a way that simulates an HTTP redirect, using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Location\/replace\">window.location.replace()<\/a> is your best bet. In this case, the current page will not be stored in the session history. This means that users won\u2019t be stuck in a loop if they press the back button. Here are a few examples for redirecting users to another page using <code>window.location.replace()<\/code>. The current page before the redirect is <code>https:\/\/tutorialio.com\/javascript\/faqs\/<\/code>.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">\/\/ This will redirect to https:\/\/tutorialio.com\nwindow.location.replace(\"\/\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/google.com\nwindow.location.replace(\"google.com\");\n\n\/\/ This will redirect to https:\/\/google.com\nwindow.location.replace(\"https:\/\/google.com\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/php\/faqs\/\nwindow.location.replace(\"php\/faqs\/\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/php\/faqs\/\nwindow.location.replace(\"\/php\/faqs\/\");<\/code><\/pre>\n<p>You should use <code>window.location.replace()<\/code> if you don\u2019t want your users to press back button to go to the original page which redirected them. I was not able to visit <code>https:\/\/tutorialio.com\/javascript\/faqs\/<\/code> by pressing the back button because the page was not stored in session history.<\/p>\n<p>One important point that you should remember is that the redirect will fail in case of a security violation. This generally happens when the script calling <code>window.location.replace()<\/code> is located on another domain which is different from the domain of the current page.<\/p>\n<h2 id=\"redirect-users-to-another-webpage-using-windowlocationassign\">Redirect Users to Another Webpage Using window.location.assign()<\/h2>\n<p>You can also use <code>window.location.assign()<\/code> in order to redirect users to another page. This method is similar to using <code>window.location.href<\/code> because it simulates a link click and the current page is stored in the session history. This means that the users will be able to click on the back button in browser to go to the original page that redirected them. Here are a few examples for redirecting users to another page using <code>window.location.replace()<\/code>. The current page before the redirect is <code>https:\/\/tutorialio.com\/javascript\/faqs\/<\/code>.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">\/\/ This will redirect to https:\/\/tutorialio.com\nwindow.location.assign(\"\/\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/google.com\nwindow.location.assign(\"google.com\");\n\n\/\/ This will redirect to https:\/\/google.com\nwindow.location.assign(\"https:\/\/google.com\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/javascript\/faqs\/php\/faqs\/\nwindow.location.assign(\"php\/faqs\/\");\n\n\/\/ This will redirect to https:\/\/tutorialio.com\/php\/faqs\/\nwindow.location.assign(\"\/php\/faqs\/\");<\/code><\/pre>\n<p>In all these cases, I was able to visit <code>https:\/\/tutorialio.com\/javascript\/faqs\/<\/code> by pressing the back button because the page was stored in the session history.<\/p>\n<p>Just like <code>window.location.replace()<\/code>, the redirect with <code>window.location.assign()<\/code> will fail in case of a security violation. This generally happens when the script calling <code>window.location.assign()<\/code> is located on another domain which is different from the domain of the current page.<\/p>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can use <code>window.location.href<\/code> to redirect users to another page in a way that stores the current page in session history. This will allow users to go back to the original page.<\/li>\n<li>You can also use <code>window.location.assign()<\/code> redirect your users to another page. It will also simulate a link click and store the current page in session history. It differs from <code>window.location.replace<\/code> in the sense that it will fail in case of a security violation.<\/li>\n<li>If you want to redirect users to another page in a way that doesn\u2019t store the current page in session history, you should use <code>window.location.replace()<\/code>. This simulates an HTTP redirect and users will not be able to go back to the original page by clicking the back button.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to redirect to another webpage in JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many situations where you might want to redirect users to a different page based on some action they took. For example, if the users deleted a post, you might want to redirect them to the homepage after deletion. In this tutorial, you will learn about different methods that can be used to redirect [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[19],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-how-to-guides","tag-javascript"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":10232,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions\/10232"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}