Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    hom
    hom is offline
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Edit: having self doubt about what I was going to sugest!
    Last edited by low tech; 07-22-2011 at 07:39 AM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.
    Last edited by Philip M; 07-22-2011 at 09:36 AM.

    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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);

  • Users who have thanked bullant for this post:

    webdev1958 (08-13-2011)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •