{"id":3468,"date":"2022-02-01T00:00:41","date_gmt":"2022-02-01T00:00:41","guid":{"rendered":"https:\/\/code-boxx.com\/?p=3468"},"modified":"2023-07-03T02:00:22","modified_gmt":"2023-07-03T02:00:22","slug":"redirect-parameters-javascript","status":"publish","type":"post","link":"https:\/\/code-boxx.com\/redirect-parameters-javascript\/","title":{"rendered":"Redirect With Parameters In Javascript (Simple Examples)"},"content":{"rendered":"<p>Welcome to a tutorial on how to redirect with parameters in Javascript. Need to redirect the user to another page and pass some data along?<\/p>\n<div class=\"sec-head answer\">\n<p>To redirect with parameters in Javascript:<\/p>\n<ul>\n<li><code>var query = new URLSearchParams();<\/code><\/li>\n<li><code>query.append(\"KEY\", \"VALUE);<\/code><\/li>\n<li><code>location.href = \"http:\/\/site.com\/page?\" + query.toString();<\/code><\/li>\n<\/ul>\n<\/div>\n<p>That covers the quick basics, but read on for more examples!<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-101\"><\/div>\n<p>&nbsp;<\/p>\n<h2 class=\"in-head-d\">TLDR &#8211; QUICK SLIDES<\/h2>\n<p>[web_stories_embed url=&#8221;https:\/\/code-boxx.com\/web-stories\/redirect-parameters-javascript\/&#8221; title=&#8221;Redirect With Parameters In Javascript&#8221; poster=&#8221;https:\/\/code-boxx.com\/wp-content\/uploads\/2021\/11\/STORY-JS-20230518.webp&#8221; width=&#8221;360&#8243; height=&#8221;600&#8243; align=&#8221;center&#8221;]<\/p>\n<p style=\"text-align: center;\">Fullscreen Mode &#8211; <a href=\"https:\/\/code-boxx.com\/web-stories\/redirect-parameters-javascript\/\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2 class=\"in-head-d\">TABLE OF CONTENTS<\/h2>\n<div class=\"toc\">\n<div><a href=\"#sec-params\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-refresh-ccw.svg\" width=\"36\" height=\"36\" \/>Redirect Parameters<\/a><\/div>\n<div><a href=\"#sec-download\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-download.svg\" width=\"36\" height=\"36\" \/>Download &amp; Notes<\/a><\/div>\n<div><a href=\"#sec-extra\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-plus-square.svg\" width=\"36\" height=\"36\" \/>Extra Bits &amp; Links<\/a><\/div>\n<div><a href=\"#sec-end\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-thumbs-up.svg\" width=\"36\" height=\"36\" \/>The End<\/a><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-102\"><\/div>\n<p>&nbsp;<\/p>\n<div id=\"sec-params\" class=\"sec-head\" style=\"background: #e0f3ff;\">\n<h2 class=\"in-head-d\">REDIRECT WITH PARAMETERS<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-refresh-ccw.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>All right, let us now get into more examples on how to redirect with parameters in Javascript.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">1) HTML FORM ONLY<\/span><\/h3>\n<div class=\"ref-head\">1-form-only.html<\/div>\n<pre><code>&lt;form action=\"1-form-only.html\"&gt;\r\n  &lt;input type=\"text\" name=\"name\" required value=\"Jon\"&gt;\r\n  &lt;input type=\"email\" name=\"email\" required value=\"jon@doe.com\"&gt;\r\n  &lt;input type=\"submit\" value=\"Go\"&gt;\r\n&lt;\/form&gt;<\/code><\/pre>\n<p>A quick reminder to those who may have forgotten, we don&#8217;t actually need Javascript to &#8220;redirect with parameters&#8221;. Just create an HTML form, give all the fields a <code>name<\/code>. The browser will do the rest of the magic on submission &#8211; This will &#8220;redirect&#8221; to\u00a0 <code>http:\/\/your-site.com\/1-form-only.html?name=Jon&amp;email=jon%40doe.com<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">2) URL SEARCH PARAMETERS<\/span><\/h3>\n<div class=\"ref-head\">2-search.params.html<\/div>\n<pre><code>&lt;!-- (A) JAVASCRIPT --&gt;\r\n&lt;script&gt;\r\nfunction redirect () {\r\n  \/\/ (A1) URL SEARCH PARAMETERS\r\n  var query = new URLSearchParams();\r\n  query.append(\"name\", \"Jon\");\r\n  query.append(\"email\", \"jon@doe.com\");\r\n \r\n  \/\/ (A2) APPEND TO URL\r\n  location.href = \"2-search-params.html?\" + query.toString();\r\n}\r\n&lt;\/script&gt;\r\n \r\n&lt;!-- (B) TEST BUTTON --&gt;\r\n&lt;button onclick=\"redirect()\"&gt;Go&lt;\/button&gt;<\/code><\/pre>\n<p>As in the introduction above:<\/p>\n<ul>\n<li>We create a <code>var query = new URLSearchParams()<\/code> object.<\/li>\n<li>Use <code>query.append(\"KEY\", \"VALUE\")<\/code> to add parameters.<\/li>\n<li>Lastly, <code>query.toString()<\/code> to append all the parameters behind your URL.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-103\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">3) MANUAL PARAMETERS<\/span><\/h3>\n<div class=\"ref-head\">3-manual.html<\/div>\n<pre><code>&lt;script&gt;\r\nfunction redirect () {\r\n  \/\/ (A1) SEARCH PARAMETERS\r\n  var params = \"name=\" + encodeURIComponent(\"Jon\");\r\n  params += \"&amp;email=\" + encodeURIComponent(\"jon@doe.com\");\r\n \r\n  \/\/ (A2) URL WITH SEARCH PARAMETERS\r\n  var url = \"3-manual.html?\" + params;\r\n \r\n  \/\/ (A3) REDIRECT\r\n  location.href = url;\r\n}\r\n&lt;\/script&gt;\r\n \r\n&lt;!-- (B) TEST BUTTON --&gt;\r\n&lt;button onclick=\"redirect()\"&gt;Go&lt;\/button&gt;<\/code><\/pre>\n<p>Lastly, here is the old-school way to manually build the URL and query string.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"sec-download\" class=\"sec-head\" style=\"background: #ffe8de;\">\n<h2 class=\"in-head-d\">DOWNLOAD &amp; NOTES<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-download.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>Here is the download link to the example code, so you don&#8217;t have to copy-paste everything.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">SORRY FOR THE ADS...<\/span><\/h3>\r\n<p>But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a \"paid scripts\" business, and I don't \"block people with Adblock\". Every little bit of support helps.<\/p>\r\n\r\n<p><a class=\"ast-button\" href=\"https:\/\/www.paypal.com\/paypalme\/wstoh\/5\" target=\"_blank\" rel=\"nofollow noopener\">Buy Me A Coffee<\/a> <a class=\"ast-button\" href=\"https:\/\/payhip.com\/codeboxx\" target=\"_blank\" rel=\"nofollow noopener\">Code Boxx eBooks<\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE CODE DOWNLOAD<\/span><\/h3>\n<p><a href=\"https:\/\/gist.github.com\/code-boxx\/98665062718708a91d2fece507b8666d\" target=\"_blank\" rel=\"noopener noreferrer\">Click here for the source code on GitHub gist<\/a>, just click on &#8220;download zip&#8221; or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"sec-extra\" class=\"sec-head\" style=\"background: #f8d5ff;\">\n<h2 class=\"in-head-d\">EXTRA BITS &amp; LINKS<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-plus-square.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>That&#8217;s all for the tutorial, and here is a small section on some extras and links that may be useful to you.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">WHICH IS THE BEST METHOD?<\/span><\/h3>\n<p>Personally, I will stick with the modern <code>new URLSearchParams()<\/code>. This is just a lot more convenient and easier to work with. But if you have to support ancient browsers, the hidden form is not a bad idea.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-106\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">LINKS &amp; REFERENCES<\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/code-boxx.com\/build-query-string-javascript\/\" target=\"_blank\" rel=\"noopener\">How To Build Query Strings In Javascript<\/a> &#8211; Code Boxx<\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/URLSearchParams\" target=\"_blank\" rel=\"noopener\">URL Search Params<\/a> &#8211; MDN<\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/encodeURIComponent\" target=\"_blank\" rel=\"noopener\">Encode URI Component<\/a> &#8211; MDN<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">INFOGRAPHIC CHEAT SHEET<\/span><\/h3>\n<figure id=\"attachment_1901\" aria-describedby=\"caption-attachment-1901\" style=\"width: 312px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2022\/02\/JS-redirect-params230703-R.webp\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1901 size-full\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2022\/02\/JS-redirect-params230703-sm-R.webp\" alt=\"\" width=\"312\" height=\"600\" \/><\/a><figcaption id=\"caption-attachment-1901\" class=\"wp-caption-text\"><span style=\"font-size: 16px;\">Javascript Redirect With Parameters (click to enlarge)<\/span><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<div id=\"sec-end\" class=\"sec-head\" style=\"background: #f1f1f1;\">\n<h2 class=\"in-head-d\">THE END<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-thumbs-up.svg\" width=\"80\" height=\"80\" \/><\/p>\n<p>Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to a tutorial on how to redirect with parameters in Javascript. Need to redirect the user to another page and pass some data along? To redirect with parameters in Javascript: var query = new URLSearchParams(); query.append(&#8220;KEY&#8221;, &#8220;VALUE); location.href = &#8220;http:\/\/site.com\/page?&#8221; + query.toString(); That covers the quick basics, but read on for more examples! &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":21210,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[],"class_list":["post-3468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/3468"}],"collection":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/comments?post=3468"}],"version-history":[{"count":0,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/3468\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media\/21210"}],"wp:attachment":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media?parent=3468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/categories?post=3468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/tags?post=3468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}