Created
May 19, 2011 19:24
-
-
Save kirbysayshi/981510 to your computer and use it in GitHub Desktop.
Detect if Flash is blocked (On-Demand) in Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flashAvail({ waitFor: 10000 }) | |
.done( function(result){ | |
if(result.flashBlocked === false){ | |
// do whatever you need flash for | |
} else { | |
// flash is installed but blocked. present messaging | |
alert('Oops!', | |
'It appears that Flash is being blocked. \ | |
Flash is required for Grill Circle and Facebook. \ | |
Please enable it and refresh the page.'); | |
} | |
}) | |
.fail( function(result){ | |
// flash is not installed | |
alert('Oops!', | |
'Flash does not appear to be installed. \ | |
Flash is required for Grill Circle and Facebook. \ | |
Please install it and refresh the page.'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
/* | |
* Requires jQuery >= 1.5 and swfobject | |
* | |
* Options: | |
* swfUrl: the url of the swf to load. default: 'assets/flash/BlockTest.swf' | |
* waitFor: how long to poll the swf before giving up. default: 2000 | |
* lookFor: the name of the method that the swf exposes. this method | |
* can return whatever it likes. default: 'returnsTrue' | |
* | |
* Returns a Deferred, resolved if Flash is installed, rejected if not. | |
* If resolved, returns { | |
* flashBlocked: bool // true is flash is installed but blocked | |
* swfobj: object // the original argument swfobject returned on success | |
* } | |
*/ | |
var flashAvail = function (options){ | |
var settings = $.extend({ | |
swfUrl: 'assets/flash/BlockTest.swf' | |
,waitFor: 2000 | |
,lookFor: 'returnsTrue' | |
}, options) | |
,repId = 'swfextBlockTest' + (0|Math.random()*10000) | |
,repEl = document.createElement('div') | |
,dfdResult = new $.Deferred(); | |
// make a quick div to fill | |
repEl.style.display = 'none'; | |
repEl.id = repId; | |
document.body.appendChild(repEl); | |
swfobject.embedSWF( | |
settings.swfUrl | |
,repId | |
,1, 1 | |
,'9.0.0' | |
,false | |
,false | |
,false | |
,{ | |
name: repId | |
,allowScriptAccess: "always" | |
} | |
,swfEmbedCallback); | |
function swfEmbedCallback(e){ | |
// e = { success: bool, id: string, ref: html object element } | |
var fo | |
,start = +new Date() | |
,interval; | |
if(e.success === true){ | |
// flash is installed/enabled, try calling generic method to test flash block | |
// poll for the flash to be ready, but only until the waitFor limit | |
interval = setInterval(function onPoll(){ | |
if(e.ref && e.ref[settings.lookFor]){ | |
// flash is installed, enabled, and accessible: not blocked | |
clearInterval(interval); | |
dfdResult.resolve({ flashBlocked: false, swfobj: e }); | |
document.body.removeChild(e.ref); | |
} else if(+new Date() - start > settings.waitFor){ | |
// flash has not responded in the allotted time, assume | |
// it's blocked, but installed | |
clearInterval(interval); | |
dfdResult.resolve({ flashBlocked: true, swfobj: e }); | |
document.body.removeChild(e.ref); | |
} | |
}, 100); | |
} else { | |
// swfobj was not successful, flash is probably not installed | |
// this could also be triggered from a bad path? | |
dfdResult.reject(e); | |
} | |
}; | |
return dfdResult; | |
} | |
return flashAvail; | |
}); |
The 'settings.lookFor' represent the name of the function inside the swf (look at ExternalInterface).
The swf should be embaded with bigger size otherwise chrome will block it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does the 'settings.lookFor'?