Skip to content

Instantly share code, notes, and snippets.

@amccloud
Created February 6, 2013 17:29
Show Gist options
  • Save amccloud/4724235 to your computer and use it in GitHub Desktop.
Save amccloud/4724235 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
var cacheBust = 0,
hashInterval,
lastHash;
this.xd = {
postMessage: function(message, targetOrigin, otherWindow) {
if (!targetOrigin)
return;
otherWindow = otherWindow || window.parent;
if (window.postMessage) {
otherWindow.postMessage(message, targetOrigin.replace(/([^:]+:\/\/[^\/]+).*/, '$1'));
} else {
var hash = '#' + (+new Date()) + (cacheBust++) + '&' + message;
otherWindow.location = targetOrigin.replace(/#.*$/, '') + hash;
}
},
onMessage: function(callback, origin) {
if (window.postMessage) {
if (window.addEventListener)
window[callback ? 'addEventListener' : 'removeEventListener']('message', callback);
else
window[callback ? 'attachEvent' : 'detachEvent']('onmessage', callback);
} else {
if (!callback && hashInterval) {
clearInterval(hashInterval);
hashInterval = null;
}
var hashRe = /^#?\d+&/;
if (callback) {
hashInterval = setInterval(function() {
var hash = document.location.hash;
if (hash != lastHash && hashRe.test(hash)) {
lastHash = hash;
callback({
data: hashRe.replace(hash, '')
});
}
}, 100);
}
}
}
};
}).call(this);
@catink123
Copy link

xd

@antoniocastanheira37
Copy link

xd

@FranLendsum
Copy link

xd

@pslaulhe
Copy link

xd

@fnovellafletcher
Copy link

xd

@Hc747
Copy link

Hc747 commented Jan 9, 2020

xd

@aledotsoftware
Copy link

xd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment