Test if URL is accessible from web browser i.e. make sure not blocked by Proxy server

后端 未结 5 1021
日久生厌
日久生厌 2020-12-16 19:47

I am serving my website from mywebsite.com. I host images on flickr so all images are loaded in the user\'s browser via get requests to flickr. Many of my websites users a

相关标签:
5条回答
  • 2020-12-16 20:11

    You could try this...

    var image = new Image();
    
    image.onerror = function() {
    
       var images = document
                     .getElementById('flicker-images')
                     .getElementsByTagName('img');
    
       for (var i = 0, imagesLength = images.length; i < imagesLength; i++) {
            images[i].src = 'images/flickr_is_blocked.gif';
       }
    
    };
    
    image.src = 'http://flickr.com/favicon.ico';
    

    Hacky, but it seems to work. However it relies that the favicon.ico 404ing means the main site is.

    jsFiddle.

    0 讨论(0)
  • 2020-12-16 20:11

    Working example: http://jsfiddle.net/peeter/pW5wB/

    JS:

    $(document).ready(function() {
    
        var callbackOnSuccess = function(src) {
            alert("Successfully loaded " + src);
            return false;
        };
        var callbackOnFailure = function(src) {
    
            alert("Failed loading " + src);
    
            // Here you can do whatever you want with your flickr images. Lets change the src and alt tags
            $(".flickr").attr("src", "flickr_is_blocked.gif");
            $(".flickr").attr("alt", "Flicker is blocked");
            // Lets change the parents href to #
            $(".flickr").parent().removeAttr("href");
            return false;
        };
    
        checkAvailability("http://flickr.com/favicon.ico", callbackOnSuccess, callbackOnFailure);
    
    
    });
    
    function checkAvailability(src, callbackSuccess, callbackFailure) {
        $("<img/>").attr("src", src).load(function() {
            callbackSuccess(src);
        }).error(function() {
            callbackFailure(src);
        });
    }
    

    HTML:

    <a href="http://flickr.com/favicon.ico">
        <img class="flickr" src="http://flickr.com/favicon.ico" alt="Flickr"/>
    </a>
    
    0 讨论(0)
  • 2020-12-16 20:11

    For facebook you can simply include the Facebook JS API and then test if one of the objects/functions it exports exists.

    It would be better if you did not (ab-)use external hosts for your stuff. If you want a CDN, better use a real one...

    0 讨论(0)
  • 2020-12-16 20:21

    This works, but timeout must be set!

    $.ajax({
        url: "http://example.com/ping.html",
        type: 'GET',
        dataType: 'jsonp',
        jsonpCallback: 'jsonCallback',
        timeout: 1000,
        cache: false,
        success: function(response) {
            console.log("SERVER UP!");
        },
        error: function(e) {
            console.log("SERVER DOWN!");
        }
    });
    

    ping.html should return:

    jsonCallback({response:'PONG'});
    
    0 讨论(0)
  • 2020-12-16 20:25

    Flickr and Facebook both have APIs that support JSONP, so cross-domain isn't an issue. i.e. Here's a request that just echoes some dummy data from flickr's API.

    $.ajax({
      url: "http://www.flickr.com/services/rest/?jsoncallback=?",
      dataType: 'json',
      data: {method: "fickr.test.echo", format: "json", api_key: "02de950d65ec54a7a057af0e992de790"},
      success: callback
    });
    

    You can't reliably set error handlers on a jsonp reqest, so show a "loading" image until that success callback gets called. Set some timeout that will show an error message if the response doesn't come back fast enough.

    0 讨论(0)
提交回复
热议问题