[thelist] JS: Snippet to grab GET/POST value?

Steve Lewis nepolon at worlddomination.net
Mon Aug 18 16:56:11 CDT 2003


Frank wrote:

> Can someone recommend the simplest way to retrieve a get/post value 
> using javascript?

For posts your best bet would be to add some server-side scripting to 
write the posted values to the returned document so you can detect them 
later in JS. 

server-side psuedocode:
if $request_method = "post"
{
  output = '<script language="javascript"> var request_method = "post";' 
+ newline
  for each $item in $post_array[]
  {
    output .= 'var ' + $item + ' = "' + $html_encode(post_array[$item]) 
+ '"; ' + newline
  }
  output .= '</script>
}
write.output

This would allow you to access the values submitted in the last request 
if the method == post.  If the request method == get, than like .jeff 
said it will be in the window.location

Instead of writing them to JS variables you could write them into the 
document using hidden input fields, which would be a more DOM-friendly 
solution.  In this case they should be isolated in a separate form that 
has no obvious way to be submitted with the next request.  You don't 
want to double the size of the form submit with every request.

If you need to attempt to detect that the current page was reloaded, you 
will need to involve some server-side code; and in this case you should 
take a very direct method to achieve this:
1) generate a unique request ID that is embedded in every request
2) and embed the unique request ID from the last page in there also 
(these are both server-side actions)
3) then use JS to compare them.

--Steve



More information about the thelist mailing list