Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I intercept all page postbacks in jquery before they occur?

Posted on 2014-12-04
3
180 Views
Last Modified: 2014-12-08
Hi:

I've got of client side processing on a web page which needs to be completed before the page posts back to the server.
There are several operations on the page which could result in a postback, so I would like to universally intercept the postback on the client side before it occurs, complete some processing and proceed with the postback.

What is the best way to do this in jquery?

Thanks,
JohnB
0
Comment
Question by:jxbma
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40481927
var notDone = true;
jQuery(function($) {
   $("form").submit(function(evt) {
       if(notDone) {
           evt.preventDefault();
           // do what you need here SYNCHRONOUSLY, I mean no timer, no Ajax call
           // do what you need here SYNCHRONOUSLY, I mean no timer, no Ajax call
           // do what you need here SYNCHRONOUSLY, I mean no timer, no Ajax call
           // do what you need here SYNCHRONOUSLY, I mean no timer, no Ajax call
           notDone = false;
           $(this).submit();
       }
   });
});

Open in new window

0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 40482875
if you are using $.ajax({..}) you can set the properties off all the calls that you will use with "$.ajax" function.

I.E.:
$.ajax({beforeSend:function(jqXHR,settings){
//DO SOMETHING BEFORE SEND THE DATA TO THE SERVER
}})

Open in new window

if its only a page that have forms then you can just set the "submit" event of the forms.
$('form').submit(function(){
//your process
//return false to cancel the submit
//return true to proceed with the submit
})

Open in new window

0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 40487251
Ultimately, this was the solution I chose to go with.

Thx
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question about traversing a tree using jQuery 7 33
jQuery Scroll To Top 5 37
Button function on table is in trouble 3 20
on window close, not beforeunload 6 15
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question