...

View Full Version : check 404 of a javascript



hom
07-22-2011, 05:58 AM
Hello, I have never used javascript before, so this might be a very basic question. I have a script on my blog which places a stumbleupon button on the page.

This is the code that calls the script:



<script src="http://www.stumbleupon.com/hostedbadge.php?s=5"></script>


Today I went to my blog and it was taking a long time to load, it turns out because stumbleupons site is down so the script can't be accessed. A whole lot of other stuff on the page just doesn't load until it times out though, so it takes like 3 minutes for the page to load.

What I want to know is can I test if the script is reachable and only call it if it is, so that if there is a 403 or 404 error it just won't even try to load it and continue loading the rest of the page??

Any help is greatly apprectiated!

Thanks.

low tech
07-22-2011, 07:33 AM
Edit: having self doubt about what I was going to sugest!

Philip M
07-22-2011, 07:38 AM
What I want to know is can I test if the script is reachable and only call it if it is, so that if there is a 403 or 404 error it just won't even try to load it and continue loading the rest of the page??


No, to test if the script is reachable you would have to call it. :)

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
07-23-2011, 06:06 AM
I wonder if you couldn't use an AJAX call to try to download the contents of the script? You can set the timeout for the AJAX call to a very short interval. You don't really care if you get trash as the response; you just want to test for the existence of the response.

In fact, I think I'd use a SYNCHRONOUS call (SJAX?) with a really short timeout (2 seconds?) and handle the failure gracefully.

bullant
07-23-2011, 10:59 AM
I wonder if you couldn't use an AJAX call to try to download the contents of the script? .

Not if it's on a different domain.

If it's on the same domain you could send a head request to see if the url exists.

A simplified example


var url='http://www.yourdomain.com';
xmlHttp = new XMLHttpRequest();
xmlHttp.open('HEAD', url, false);
xmlHttp.send();
alert(xmlHttp.status);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum