Solved

How to run a javascript before postback which is called on window load

Posted on 2011-09-23
7
352 Views
Last Modified: 2012-05-12
Hello all,

  I am working on a web application. In one of my content pages, I wanted to check the internet connection before every postback. If there is no inernet connection then the postback should not occur.

This is my function to check the internet connection.

function checkinternet()
{

 $.ajax({
  url: 'http://xxx.xxx.xx.xx/driver',
  success: function(data) {
   alert('Connection.');
  },
  error: function(data) {
    alert('No Internet Connection!');
    return false;
  }
});
}

I have a setInterval in javascript which runs another javascript function(check the internet connection and then to get the GEOCODES) every 30 seconds

  function initiate_geolocation() {  
 
         // check the internet connection
         checkinternet()
         
         navigator.geolocation.getCurrentPosition(handle_geolocation_query);  
         return false;
         }

I called the function on window load..like this..

          var OnLoad = 'initiate_geolocation()';
          window.onload = function() {eval(OnLoad);};


         window.load = setInterval(function() {eval(OnLoad);}, 30000);

My question is ,


1) I reallly want the javascript to be executed first and then the code behind based on the result in the javascript.

    I know that if it is a button then i can do onclientClick.. but I am calling this javascript on window load, How do i do this?

     I really do not want to display a error page  if there is no internet connection. I want to be in the same screen where the driver was when he lost the internet connection..

Appreciate the help

Thanks
0
Comment
Question by:shahjagat
  • 4
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Remove/comment your current code and use ONLY :

	$(document).ready(function() {
		$("form").submit(function(e) {
			var r = $.ajax({ "url": 'http://xxx.xxx.xx.xx/driver', "async":false }).responseText;
			if( r==null ) e.preventDefault();
		})
	})

Open in new window

0
 

Author Comment

by:shahjagat
Comment Utility
Thank you for the reply....

I replaced my internet function with your code... Now the postback is not happening.. But i jave a question here..

If you give document.ready then it will execute after the page is loaded completely.. Correct me if I am wrong....

Thanks
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
it work?
0
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.

 

Author Comment

by:shahjagat
Comment Utility
Im testing it completely... will let you know...

Thanks
0
 

Author Comment

by:shahjagat
Comment Utility
leakim971,

It worked.
If you dont mind, can you explain me these 2 lines? I am not familiar with these.
var r = $.ajax({ "url": 'http://xxx.xxx.xx.xx/driver', "async":false }).responseText;
                  if( r==null ) e.preventDefault();


what does the first line return?
why at is repornseText?

Next line you are checking r== null. When would r be null and what does e.preventdefault do?

Thanks.

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
ok, good news

>var r = $.ajax({ "url": 'http://xxx.xxx.xx.xx/driver', "async":false }).responseText;

this is an synchronous ajax call : http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
just like a usual one, but we wait end of the call before going to the next line of code

>if( r==null ) e.preventDefault();

if the result of the ajax call is null (no answer) we stop submission of the form (we stop the postback)
http://api.jquery.com/event.preventDefault/
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 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

8 Experts available now in Live!

Get 1:1 Help Now