Redirect With Parameters In Javascript (Simple Examples)

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("KEY", "VALUE);
  • location.href = "http://site.com/page?" + query.toString();

That covers the quick basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Redirect Parameters Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

REDIRECT WITH PARAMETERS

All right, let us now get into more examples on how to redirect with parameters in Javascript.

 

1) HTML FORM ONLY

1-form-only.html
<form action="1-form-only.html">
  <input type="text" name="name" required value="Jon"/>
  <input type="email" name="email" required value="jon@doe.com"/>
  <input type="submit" value="Go"/>
</form>

A quick reminder to those who may have forgotten, we don’t actually need Javascript to “redirect with parameters”. Just create an HTML form, give all the fields a name. The browser will do the rest of the magic on submission – This will “redirect” to  http://your-site.com/1-form-only.html?name=Jon&email=jon%40doe.com.

 

2) URL SEARCH PARAMETERS

2-search.params.html
<!-- (A) JAVASCRIPT -->
<script>
function redirect () {
  // (A1) URL SEARCH PARAMETERS
  var query = new URLSearchParams();
  query.append("name", "Jon");
  query.append("email", "jon@doe.com");
 
  // (A2) APPEND TO URL
  location.href = "2-search-params.html?" + query.toString();
}
</script>
 
<!-- (B) TEST BUTTON -->
<button onclick="redirect()">Demo</button>

As in the introduction above:

  • We create a var query = new URLSearchParams() object.
  • Use query.append("KEY", "VALUE") to add parameters.
  • Lastly, query.toString() to append all the parameters behind your URL.

 

 

3) MANUAL PARAMETERS

3-manual.html
<script>
function redirect () {
  // (A1) SEARCH PARAMETERS
  var params = "name=" + encodeURIComponent("Jon");
  params += "&email=" + encodeURIComponent("jon@doe.com");
 
  // (A2) URL WITH SEARCH PARAMETERS
  var url = "3-manual.html?" + params;
 
  // (A3) REDIRECT
  location.href = url;
}
</script>
 
<!-- (B) TEST BUTTON -->
<button onclick="redirect()">Demo</button>

Lastly, here is the old-school way to manually build the URL and query string.

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

WHICH IS THE BEST METHOD?

Personally, I will stick with the modern new URLSearchParams(). This is just a lot more convenient and easier to work with. But if you have to support the ancient browsers, the hidden form is not a bad idea.

 

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Javascript Redirect With Parameters (click to enlarge)

 

THE END

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!

Leave a Comment

Your email address will not be published.