HiDd3N
HiDd3N

Reputation: 494

How can I stop a page from loading if an external script can't be loaded?

I want a function that stops a page from loading if Google Plus One couldn't load in 5 seconds. In our country sometimes the HTTPS port is blocked by the telecommunications department and we don't have access to apis.google.com. It is not just slow, we realy don't have access to the HTTPS port. If this is the case I want some way of stopping the page from loading when we can't access https://apis.google.com/js/plusone.js. How can I do this?

Upvotes: 1

Views: 321

Answers (2)

adeneo
adeneo

Reputation: 318222

Just check to see if the Plusone script loads within 5 seconds, like so:

var i=0, checkPlusone = setInterval(function() {
    if (typeof gapi!='undefined') {
        if (gapi.hasOwnProperty('plusone')) {
             clearInterval(checkPlusone);
             console.log('Plusone loaded');
        }
    }
    if (i>5000) {
         clearInterval(checkPlusone);   
         console.log('Plusone not loaded');
         window.location = "http://www.mypage.com/no_plusone.html"; //redirect to error page
    }
    i=i+300;
}, 300);

FIDDLE

Upvotes: 1

HappyTimeGopher
HappyTimeGopher

Reputation: 1387

You could load the file with ajax and use a timeout.

Ctrl+F for "timeout" on the jQuery ajax docs page, and see it in use in this SO question.

Upvotes: 0

Related Questions