Page reload or not in a JavaScript function after a POST

There is a subtle difference when I do a POST action within a JavaScript function. If I want the page to reload without any of the GET parameters, then I do the following:

In HTML:

onSubmit="myfunction()"

In JavaScript

var myfunction = function(){
  // do POST action
}

If I want the page to not reload after the POST action, then I will have add a few key words.

In HTML:

onSubmit="return myfunction()"

In JavaScript

var myfunction = function(){
  // do POST action
  return false;
}

Special thanks to Rinaldo D. for this tip.

How about doing a page reload with all the GET parameters within JavaScript? If you know how, please leave me a comment. Thanks.

Advertisements

2 thoughts on “Page reload or not in a JavaScript function after a POST

  1. I remember reading this several years ago when I first learned JavaScript but I had long since forgotten it. Now with the rise of AJAX this kind of feedback can be very useful for performing server-side validation and return results to the client without a submission.

    Great tip!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s