{"id":7706,"date":"2019-06-15T09:56:52","date_gmt":"2019-06-15T13:56:52","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=7706"},"modified":"2021-12-08T11:03:15","modified_gmt":"2021-12-08T16:03:15","slug":"redirect-webpage-javascript-jquery","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/","title":{"rendered":"How do I Redirect to Another Webpage in JavaScript or jQuery?"},"content":{"rendered":"\n

It is not uncommon to find that in web development<\/a> projects you can do certain things in more than one way. If you want to redirect one web page to another using JavaScript<\/a>, you will realize there is more than one way to correctly do it depending on the scenario.<\/p>\n\n\n\n

One of the major factors to determine which method to use depends on the sort of behavior that you are looking for.<\/p>\n\n\n\n

There are three main ways to perform the redirect that you want albeit some of these are not true redirects.<\/p>\n\n\n\n

1. The ideal method to use is the location replace<\/code> method. This method simulates a redirect and replaces the current history of the page making it impossible to use the back button to reach the original page. You can use location replace<\/code> like so:<\/p>\n\n\n\n

window.location.replace("http:\/\/local.brightwhiz");<\/code><\/pre>\n\n\n\n

2. You can use the location assigned to load a page and keep the browser history allowing you to use the back button. You can use location assign<\/code> like so:<\/p>\n\n\n\n

window.location.assign("http:\/\/local.brightwhiz");<\/code><\/pre>\n\n\n\n

3. This final option using location href<\/code> simulates clicking of a link and is not a true redirect although it can also simulate a redirect. This method also maintains the browser history allowing you to use the back button.<\/p>\n\n\n\n

window.location.href="http:\/\/local.brightwhiz";<\/code><\/pre>\n\n\n\n

Bonus; Redirect Using jQuery<\/h2>\n\n\n\n

Obviously, if you have included jQuery<\/a> on your project for other functionality, you have the option of using it to redirect to another web page. The following are examples of the methods you can use:<\/p>\n\n\n\n

$(location).attr('href','http:\/\/local.brightwhiz');\n\n$(window).attr('location','http:\/\/local.brightwhiz');\n\n$(location).prop('href', 'http:\/\/local.brightwhiz');<\/code><\/pre>\n\n\n\n

There you have it. That should get you started. Meanwhile, you can check out the official ECMAScript reference online<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

It is not uncommon to find that in web development projects you can do certain things in more than one way. If you want to redirect one web page to…<\/p>\n","protected":false},"author":1,"featured_media":7707,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,27,16],"tags":[106,170,303,320,328,333,471,543,635,638,643],"yoast_head":"\nRedirect Correctly to Another Webpage in JavaScript or jQuery?<\/title>\n<meta name=\"description\" content=\"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redirect Correctly to Another Webpage in JavaScript or jQuery?\" \/>\n<meta property=\"og:description\" content=\"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-15T13:56:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T16:03:15+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How do I Redirect to Another Webpage in JavaScript or jQuery?\",\"datePublished\":\"2019-06-15T13:56:52+00:00\",\"dateModified\":\"2021-12-08T16:03:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\"},\"wordCount\":263,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg\",\"keywords\":[\"Browsers\",\"CSS\",\"HTML\",\"Internet\",\"JavaScript\",\"jQuery\",\"Programming\",\"Software Design\",\"Web\",\"Web Development\",\"Websites\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\",\"url\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\",\"name\":\"Redirect Correctly to Another Webpage in JavaScript or jQuery?\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg\",\"datePublished\":\"2019-06-15T13:56:52+00:00\",\"dateModified\":\"2021-12-08T16:03:15+00:00\",\"description\":\"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Redirect to Another Webpage in JavaScript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I Redirect to Another Webpage in JavaScript or jQuery?\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redirect Correctly to Another Webpage in JavaScript or jQuery?","description":"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/","og_locale":"en_US","og_type":"article","og_title":"Redirect Correctly to Another Webpage in JavaScript or jQuery?","og_description":"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign","og_url":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2019-06-15T13:56:52+00:00","article_modified_time":"2021-12-08T16:03:15+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How do I Redirect to Another Webpage in JavaScript or jQuery?","datePublished":"2019-06-15T13:56:52+00:00","dateModified":"2021-12-08T16:03:15+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/"},"wordCount":263,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg","keywords":["Browsers","CSS","HTML","Internet","JavaScript","jQuery","Programming","Software Design","Web","Web Development","Websites"],"articleSection":["Articles","Guides","How To","Programming","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/","url":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/","name":"Redirect Correctly to Another Webpage in JavaScript or jQuery?","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg","datePublished":"2019-06-15T13:56:52+00:00","dateModified":"2021-12-08T16:03:15+00:00","description":"This is how you can correctly redirect to another webpage using Javascript or jQuery using location replace vs href or assign","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/04\/redirection.jpg","width":1200,"height":630,"caption":"Redirect to Another Webpage in JavaScript"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/redirect-webpage-javascript-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How do I Redirect to Another Webpage in JavaScript or jQuery?"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/twitter.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/7706"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=7706"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/7706\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/7707"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=7706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=7706"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=7706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}