Solved

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

Posted on 2011-09-23
7
367 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
[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
  • 4
  • 3
7 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36588855
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
ID: 36589100
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
ID: 36589266
it work?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:shahjagat
ID: 36589400
Im testing it completely... will let you know...

Thanks
0
 

Author Comment

by:shahjagat
ID: 36589682
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
ID: 36589889
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
ID: 36590632
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

734 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