Solved

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

Posted on 2011-09-23
7
381 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Suggested Courses

630 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