Javascript (?) help - need to check connectivity

From: Monsoir (PILOTDAN) 3 Sep 2011 13:45
To: Peter (BOUGHTONP) 6 of 18
Just checked and it's IE7. At the minute, I need to assume it has to stay that way :(
From: Monsoir (PILOTDAN) 3 Sep 2011 14:08
To: Peter (BOUGHTONP) 7 of 18
I've found this sort of thing:

code:
<img alt="Connecting..." id="ImageTest" />
 
<script type="text/javascript">
 
$('#ImageTest')
  .error(function() {
    alert('Handler for .error() called.')
  })
  .attr("src", $imageURL);
 
 
</script>


It seems to work, though I've not tested it in IE7 yet. Only I'm too thick to figure out how to keep on retrying
From: Peter (BOUGHTONP) 3 Sep 2011 14:26
To: Monsoir (PILOTDAN) 8 of 18
Use setTimeout to call itself after a short pause.

javascript code:
function setImageUrl()
{
$('#ImageTest')
  .error(function() {
    alert('Handler for .error() called.')
    setTimeout( setImageUrl , 300 )
  })
  .attr("src", $imageURL);
}
 
setImageUrl()



I guess this will work in IE7 since the image is doing the cross-domain stuff, not the script, so there's no security issues.

Hmmm, might also need to set the src to a safe value and then back to the test one for it to trigger a change. (Not sure on that.)
EDITED: 3 Sep 2011 14:29 by BOUGHTONP
From: Monsoir (PILOTDAN) 3 Sep 2011 15:54
To: ALL9 of 18
Using images seems to lock up the browser when I do it in any kind of loop :(
From: Peter (BOUGHTONP) 3 Sep 2011 16:24
To: Monsoir (PILOTDAN) 10 of 18
Browsers suck. :|

Instead of looping, could you try multiple images, with each src set in a chain after the previous one fails?

Oh, and make sure IE's shit local caching is disabled (i.e. "check every visit").
From: Monsoir (PILOTDAN) 3 Sep 2011 16:25
To: ALL11 of 18
Right, I'm getting there but if I change a variable in ".Error" it doesn't seem to make any difference. Is that by design?
From: Peter (BOUGHTONP) 3 Sep 2011 16:27
To: Monsoir (PILOTDAN) 12 of 18
Should be lowercase e for error?
From: Monsoir (PILOTDAN) 3 Sep 2011 16:42
To: Peter (BOUGHTONP) 13 of 18
Think I've sussed the problem, maybe. I've added a "didError" variable and set it to one when the error occurs. However, because it's not triggering UNTIL the error, my code is going ahead and executing before the image has failed
From: Monsoir (PILOTDAN) 3 Sep 2011 16:52
To: Peter (BOUGHTONP) 14 of 18
code:
function checkImageUrl()
{
 
$('#ImageTest')
  .error(function() {
  setTimeout("checkImageUrl()", 2000);
  })
 
  $('#ImageTest').load(function() {
  window.location.replace(redirectURL);
  });
 
  $('#ImageTest').attr("src", imageURL + "?=" + d.getTime());
 
}
 
checkImageUrl();


Trial and error, yay!
From: Monsoir (PILOTDAN) 3 Sep 2011 16:55
To: Monsoir (PILOTDAN) 15 of 18
Hmm, this does work but looking at it in FireBug, it appears to exponentially load images and I'm not really sure why :(
From: Monsoir (PILOTDAN) 3 Sep 2011 17:06
To: Monsoir (PILOTDAN) 16 of 18
Ah, every time the function is called I'm creating a new event handler. Moved them out of the function and I think I'm there.
From: Peter (BOUGHTONP) 3 Sep 2011 17:14
To: Monsoir (PILOTDAN) 17 of 18
Yay!

Course once you switch to IE7 you'll probably find some random annoying error cropping up just to be a pain. :(
From: Monsoir (PILOTDAN) 3 Sep 2011 17:24
To: Peter (BOUGHTONP) 18 of 18

You were right :D Just does nothing after the first event in IE, I guess changing the image url doesn't force a reload of the image.

 

I have, however, got it working by forcing a page refresh. I'll try and figure out how to get IE to reload the image because it's much cleaner, but at least I can make it work regardless.