Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-12-04
3
Medium Priority
?
200 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

722 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