shahjagat
asked on
How to run a javascript before postback which is called on window load
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.getC urrentPosi tion(handl e_geolocat ion_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
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.getC
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
it work?
ASKER
Im testing it completely... will let you know...
Thanks
Thanks
ASKER
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.
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.
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/
>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/
ASKER
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