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

x
?
Solved

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

Posted on 2011-09-23
7
Medium Priority
?
397 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 2000 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

715 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