-
-
Save rwaldron/581050 to your computer and use it in GitHub Desktop.
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
/*! | |
* postMessage detector - v0.1pre - 9/15/2010 | |
* http://benalman.com/ | |
* | |
* Copyright (c) 2010 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
*/ | |
// If `window.postMessage.complex` is true, postMessage can pass complex non-string | |
// values like objects and arrays. If it's false, it can only pass strings (TODO: test) | |
// | |
// Note that since postMessage executes asynchronously, this value isn't available | |
// immediately... Ugh. | |
(function(window,message,postMessage){ | |
var tmp, | |
timeout_id = setTimeout(function(){ | |
handler({}); | |
},1); | |
function handler(e){ | |
clearTimeout( timeout_id ); | |
tmp = e.data; | |
if ( window.postMessage ) { | |
window.postMessage.complex = tmp && typeof tmp != 'string' && tmp.t; | |
} | |
if ( tmp = window.removeEventListener ) { | |
tmp( message, handler, false ); | |
} else if ( tmp = window.detachEvent ) { | |
tmp( 'on' + message, handler ); | |
} | |
}; | |
if ( postMessage in window ) { | |
if ( tmp = window.addEventListener ) { | |
tmp( message, handler, false ); | |
} else if ( tmp = window.attachEvent ) { | |
tmp( 'on' + message, handler ); | |
} | |
window[ postMessage ]( { t: true }, '*' ); | |
} | |
})(this,'message','postMessage'); | |
// Usage | |
setTimeout(function(){ | |
alert([ | |
'window.postMessage: ' + !!window.postMessage, | |
'window.postMessage.complex: ' + ( window.postMessage && window.postMessage.complex ) | |
].join('\n')) | |
}, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment