Solved

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

Posted on 2011-09-23
7
361 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Binding a Grid in Javascript 5 43
Character counter breaks after adding EmojiOne Area 4 19
Visual Studio npm 1 11
Fix Form size HTML 16 13
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

856 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