Solved

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

Posted on 2014-12-04
3
175 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 29
css, html 6 31
Put some white space between images in WordPress footer content 2 28
Test ddwrt:UserLookup 1 21
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now