Detecting if YouTube is blocked by company / ISP

11,739

Solution 1

I like lacker's solution, but yes, it creates a race condition. This will work and won't create a race contition:

var image = new Image();
image.onload = function(){
// The user can access youtube
};
image.onerror = function(){
// The user can't access youtube
};
image.src = "http://youtube.com/favicon.ico";

Solution 2

You can load an image from youtube using javascript and check its properties. The favicon is tiny and has a consistent url -

var image = new Image();
image.src = "http://youtube.com/favicon.ico";
if (image.height > 0) {
    // The user can access youtube
} else {
    // The user can't access youtube
}

I think this is slightly better than loading javascript because this won't try to run any code, and while youtube might rename their javascript files, or functions from those files, they are unlikely to ever rename their favicon.

Solution 3

This should work. Basically, it loads a youtube.com javascript file, then checks if a function in that file exists.

<html>

<head>
    <script src="http://www.youtube.com/js/account.js"></script>
    <script>
        function has_you_tube()
        {
            if(typeof addVideosToQuicklist == 'function')
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    </script>

</head>
<body>
    <script>alert( "has_youtube: " + has_you_tube() ); </script>
</body>


</html>

Solution 4

I got stuck on this today and tried the favicon test but it wasnt working in IE. I was using the YouTube Player API Reference for iframe Embeds to embed youtube videos into my site so what I did is perform a check on the player var defined just before the onYouTubeIFrameReady with a delay on the javascript call.

<script> function YouTubeTester() {            
            if (player == undefined) {
                alert("youtube blocked");
            }
        }
</script>
<script>window.setTimeout("YouTubeTester()", 500);</script>

Seems to work for me. I needed the delay to get it to work in IE.

Share:
11,739
aruno
Author by

aruno

Updated on June 13, 2022

Comments

  • aruno
    aruno about 2 years

    We have YouTube videos on a site and want to detect if it is likely that they will not be able to view them due to (mostly likely) company policy or otherwise.

    We have two sites:

    1) Flex / Flash 2) HTML

    I think with Flex I can attempt to download http://youtube.com/crossdomain.xml and if it is valid XML assume the site is available

    But with HTML I don't know how to do it. I can't even think of a 'nice hack'.