Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Google extension and check internet connection

connection

  • Please log in to reply
5 replies to this topic

#1 Faja

Faja

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Location:Rybnik, Poland
  • Learning:C++, C#, PHP, Python, PL/SQL

Posted 03 September 2012 - 12:27 PM

Hey, I want to check internet connection in chrome extension I have some code in javascript that is checking connection but in background.js I struggle with problem because when internet connection is ok then then alert is showing, but if there is not internet Nothing appear.


var ImageObject = new Image();
	
	 ImageObject.onload=function(){
		if(ImageObject.height > 0){
			alert("there is");
		} else {
			alert("there isn't");
		}
		};
		
ImageObject.src = "http://images?"+Math.random().toString().slice(3);

But I tested in debbuger,on the first there I have internet connection on second I don't have. And now I want to recognize this redirect seen on image2 just after get image. Any idea?

Attached Thumbnails

  • image1.png
  • image2.png

  • 0
"With great power, comes great responsibility"

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 03 September 2012 - 10:36 PM

but if there is not internet Nothing appear.

-->

onabort - Code is executed when user aborts the downloading of the image.
onerror - Code is executed when an error occurs with the loading of the image (ie: not found).
onload - Code is executed when the image successfully and completely downloads.

http://www.javascrip...ref/image.shtml
  • 0

#3 Faja

Faja

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Location:Rybnik, Poland
  • Learning:C++, C#, PHP, Python, PL/SQL

Posted 04 September 2012 - 04:16 AM

Is there possible to check somehow in code status of image? I mean text: 200 and 302 :-)
  • 0
"With great power, comes great responsibility"

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 September 2012 - 04:23 AM

hmmm not that I know of.

2 questions:
- Is the only purpose of the image checking whether you got internet or not
- Do you have JQuery available?

The reason you get the 302 redirect, is actually just your browser using the cache. It doesn't have to mean you're not online... I think.
Have that "Math.random().toString().slice(3);" for icon.png, and I guess you'll rather get 200 or 404 instead.

edit: nvm ^^

Edited by wim DC, 04 September 2012 - 04:32 AM.

  • 0

#5 Faja

Faja

    CC Newcomer

  • Member
  • PipPip
  • 10 posts
  • Location:Rybnik, Poland
  • Learning:C++, C#, PHP, Python, PL/SQL

Posted 04 September 2012 - 05:41 AM

- Is the only purpose of the image checking whether you got internet or not

Yes, only.

- Do you have JQuery available?

Nope I don't have.

Browser can't get acces to cache and dispaly image from there because there isn't this image because of random thing and every time browser think its another image... I think.
  • 0
"With great power, comes great responsibility"

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 September 2012 - 05:48 AM

Okay, you don't really need JQuery it'd just make ajax calls a bit shorter to write

var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();//decent browsers
} catch (e){// Internet Explorer
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) { // lower IE
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){ //we be screwed
alert("Can't use ajax.");
return false;
}
}
}


ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){ //4 == request complete
alert(ajaxRequest.status); //200, 302, 404,... this is the http response code
}

}
ajaxRequest.open("GET", "icon.png?noCache=" + Math.random().toString().slice(3), true); //the smaller the resource the better.
ajaxRequest.send();

Bold parts are most important and could/should be changed.

Leave purple stuff :love:

I'm not sure how the extension behaves with cross origin restrictions.(http://en.wikipedia....e_origin_policy)
Are you sure the imageObject.onload and onerror isn't enough? (onload for internet, onerror for no connection)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download