Solved

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

Posted on 2014-12-04
3
184 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
[X]
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
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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 …

740 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