An easy way to do 1 and 2 way communication across multiple frames/windows. Supports adding events programatically, and triggering events at any time after being instantiated.
You can view all options in the README: README.md
This pageEnhancedPostMessage({ sources: { oneWaySource: document.getElementbyId('oneWayIframe') } }); document.getElementById('oneWayButton').addEventListener('click', function(){ EnhancedPostMessage.trigger('oneWayEvent', 'oneWaySource'); });
The iframeEnhancedPostMessage({ listeners: { oneWayEvent: function(){ clickCounter++; document.getElementById('clickCounter').innerHTML = clickCounter.toString(); } } });
This pageEnhancedPostMessage({ sources: { oneWayWithDataSource: document.getElementbyId('oneWayWthDataIframe') } }); var oneWayWithDataInput = document.getElementById('oneWayWithDataInput'); oneWayWithDataInput.addEventListener('keyup', function(){ EnhancedPostMessage.trigger('oneWayWithDataEvent', 'oneWayWithDataSource', oneWayWithDataInput.value); });
The iframeEnhancedPostMessage({ listeners: { oneWayWithDataEvent: function(text){ document.getElementById('typedText').innerHTML = text; } } });
This pageEnhancedPostMessage({ sources: { twoWaySource: document.getElementbyId('twoWayIframe') }, listeners: { twoWayIframeEvent: function(){ iframeClickCounter++; document.getElementById('iframeClickedCount').innerHTML = iframeClickCounter.toString(); } } }); document.getElementById('twoWayButton').addEventListener('click', function(){ EnhancedPostMessage.trigger('twoWayEvent', 'twoWaySource'); });
The iframeEnhancedPostMessage({ listeners: { twoWayEvent: function(){ clickCounter++; document.getElementById('clickCounter').innerHTML = clickCounter.toString(); } }, events: { twoWayIframeEvent: true } }); document.getElementById('twoWayIframeButton').addEventListener('click', function(){ // Note the parent string as the source, but no sources are defined. // `parent` is a predefined source that will use `window.parent` as the source. EnhancedPostMessage.trigger('twoWayIframeEvent', 'parent'); });
Two with parent
button clicked 0 times.This pageEnhancedPostMessage({ sources: { twoWayWithDataSource: document.getElementbyId('twoWayWthDataIframe') }, listeners: { twoWayWithDataIframeEvent: function(text){ document.getElementById('parentTwoWayInput').innerHTML = text; } } }); var parentTwoWayInput = document.getElementById('parentTwoWayInput'); parentTwoWayInput.addEventListener('keyup', function(){ EnhancedPostMessage.trigger('twoWayWithDataEvent', 'twoWayWithDataSource', parentTwoWayInput.value); });
The iframeEnhancedPostMessage({ listeners: { twoWayWithDataEvent: function(text){ document.getElementById('iframeTwoWayInput').value = text; } } }); var iframeTwoWayInput = document.getElementById('iframeTwoWayInput'); iframeTwoWayInput.addEventListener('click', function(){ // Note the parent string as the source, but no sources are defined. // `parent` is a predefined source that will use `window.parent` as the source. EnhancedPostMessage.trigger('twoWayIframeEvent', 'parent', iframeTwoWayInput.value); });
This pageEnhancedPostMessage.addSource('dynamicSourceName', document.getElementById('dynamicSource')); var iframe = document.createElement('iframe'); iframe.onload = function(){ EnhancedPostMessage.trigger('dynamicEventName', 'dynamicSourceName'); }; document.getElementsByTagName('body')[0].appendChild(iframe);
The Iframevar callback = function(){ // Do something amazing }; EnhancedPostMessage.addListener('dynamicEventName', callback);
EnhancedPostMessage({
sources: {
oneWay: document.getElementById('oneWayIframe'),
oneWayWithData: document.getElementById('oneWayWithDataIframe'),
twoWay: document.getElementById('twoWayIframe'),
twoWayWithData: document.getElementById('twoWayWithDataIframe')
},
listeners: {
twoWay: updateIframeClickedCount,
twoWayWithData: updateTwoWayWithDataInputValue
},
stringify: true,
debug: true
});