Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-22-2011, 05:58 AM   PM User | #1
hom
New to the CF scene

 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
hom is an unknown quantity at this point
check 404 of a javascript

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:

Code:
<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.
hom is offline   Reply With Quote
Old 07-22-2011, 07:33 AM   PM User | #2
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Edit: having self doubt about what I was going to sugest!

Last edited by low tech; 07-22-2011 at 07:39 AM..
low tech is offline   Reply With Quote
Old 07-22-2011, 07:38 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by hom View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 07-22-2011 at 09:36 AM..
Philip M is offline   Reply With Quote
Old 07-23-2011, 06:06 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 07-23-2011, 10:59 AM   PM User | #5
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
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
Code:
 
           var url='http://www.yourdomain.com';
            xmlHttp = new XMLHttpRequest();
            xmlHttp.open('HEAD', url, false);
            xmlHttp.send();
            alert(xmlHttp.status);
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
webdev1958 (08-13-2011)
Reply

Bookmarks

Tags
404, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:41 AM.


Advertisement
Log in to turn off these ads.