1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Posting Values using JavaScript

Join the Discussion

Questions? Comments?

When you are looking to pass values from one page to another you can use a session (if both pages use a server side language to set and retrieve the session variables), use a cookie (set and read either in JavaScript or a server side language), pass the values in the querystring on the end of the web address (which can then be read using either JavaScript or a server side language) or post the values from a form (which requires a server side language to read it).

While you need a server side language to be available to read the passed values if a session or posted values are used and need either a server side language or JavaScript to read a cookie or querystring, the requirements on the sending page when using a querystring or posting from a form are less strict since neither JavaScript nor a server side language are required to set and send values that way.

One of the disadvantages to using the querystring is that the values are passed on the end of the address where they can be easily modified by the visitors to your page. One of the disadvantages to posting the values is that it requires a form to be filled out in order to be able to post the values. Well where JavaScript is available to the sending page we can overcome both of these disadvantages by using JavaScript to post the values to the next page.

To do this what we will do is to use a JavaScript function that takes the address with the querystring attached and converts it into a form containing all hidden fields which we will then dynamically add to the end of the web page (where since it is hidden it will not be visible) and then immediately submit it.

By writing our code that calls this function the right way we can even set the link up so that it will still work without JavaScript although in that instance the only option is to use the querystring to pass the information.

Here's an example of a modified link that posts the data if JavaScript is available and uses a querystring if it isn't (of course the receiving page needs to contain server side code to read it either way.

<a href="http://www.felgall.com/htmlt59.php?parmone=i&parmtwo=j" onclick="toPost(this.href); return false;">go</a>

It is the addition of the code shown in bold that runs the JavaScript that converts the href from using a querystring to pass the values to posting them instead. Note that the href must contain a query string made up of key/value pairs in order for this to work.

The only other thing you need to do apart from adding the onclick code to the links is to add the toPost() function into the head of your page (either in an existing script file that you have linked there or in its own file.

If you want to add this functionality as a separate file then save it as topost.js and then add the following code to the head of your page.

<script type="text/javascript" src="topost.js">
Related Video
Using Meta Data for Search

©2017 About.com. All rights reserved.