Skip to content

Instantly share code, notes, and snippets.

@ryanseddon
Created January 21, 2013 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanseddon/4583494 to your computer and use it in GitHub Desktop.
Save ryanseddon/4583494 to your computer and use it in GitHub Desktop.
postMessage structured clone detection
<p id="support"></p>
Copyright (c) 2013 Ryan Seddon
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
/*
* Copyright (c) 2013 Ryan Seddon
* Licensed under the MIT license.
*/
var support = 0,
_bTestResults,
result = document.getElementById("support");
// Beacon the results to Browserscope.
var beaconResults = function() {
var resultdata = (support ? "<span style='color:#090; font-weight: bold; font-size: 1.2em;'>DOES</span>" : "<span style='color:#C00; font-weight: bold; font-size: 1.2em;'>DOES NOT</span>" );
result.innerHTML = "Your browser " + resultdata + " support structured clones";
var testKey = 'agt1YS1wcm9maWxlcnINCxIEVGVzdBiPp8ALDA';
var newScript = document.createElement('script'),
firstScript = document.getElementsByTagName('script')[0];
newScript.src = 'http://www.browserscope.org/user/beacon/' + testKey;
firstScript.parentNode.insertBefore(newScript, firstScript);
};
if(!!window.postMessage) {
try {
// Safari 5.1 will sometimes throw an exception and sometimes won't, lolwut?
// When it doesn't we capture the message event and check the
// internal [[Class]] property of the message being passed through.
// Safari will pass through DOM nodes as Null, gotcha.
window.onmessage = function(e){
support = Object.prototype.toString.call(e.data).indexOf("Null") != -1 ? 1 : 0;
_bTestResults = {
'structured_clones': support
};
beaconResults();
}
// Spec states you can't transmit DOM nodes and it will throw an error
// postMessage implimentations that support cloned data will throw.
window.postMessage(document.createElement("a"),"*");
} catch(e) {
support = e.DATA_CLONE_ERR ? 1 : 0;
_bTestResults = {
'structured_clones': support
};
beaconResults();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment