This is a PURE copy of the porthole library proposed by TernaryLabs View Original Repo to make it available via npm.
Installation
npm install porthole-proxy --save
For you convenience, here is a copy of the Readme file of the library. I only updated the
<script>
tag to make it consistent with thenode_modules
hierarchy.
Porthole is a small library for secure cross-domain iFrame communication.
Usage
Include the Javascript.
Create your content iFrame. This is where the guest content lives. Make sure to give it a name.
Define an event handler if you want to receive messages.
{ /* messageEvent.origin: Protocol and domain origin of the message messageEvent.data: Message itself messageEvent.source: Window proxy object, useful to post a response */}
Create a window proxy object on the main page.
var windowProxy;window{ // Create a proxy window to send to and receive // messages from the iFrame windowProxy = 'http://other-domain.com/proxy.html' 'guestFrame'; // Register an event handler to receive messages; windowProxy;};
Create a window proxy object in the iFrame.
var windowProxy;window{ // Create a proxy window to send to and receive // messages from the parent windowProxy = 'http://parent-domain.com/proxy.html'; // Register an event handler to receive messages; windowProxy;};
Send a message.
windowProxy;
Note that if you have multiple iFrames, you can create as many WindowProxy objects as needed.
Update
The library has been updated. It is not compatible with previous versions.
- Support JSON serialization
- Bug fix for IE 8
- Bug fix for multiple event handler in native browser support.
Unit Tests
rake jasmine
Demo
http://sandbox.ternarylabs.com/porthole/