GithubHelp home page GithubHelp logo

deankamali / bellhop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from springroll/bellhop

0.0 1.0 0.0 680 KB

Javascript event-based communication layer between DOM and iframe.

Home Page: http://springroll.github.io/Bellhop

License: MIT License

JavaScript 97.11% HTML 2.89%

bellhop's Introduction

Bellhop Build Status Dependency Status

Bellhop is a simple event-based communication layer between the page DOM and an iframe. It doesn't require any additional dependencies. Super easy to use and setup.

Installation

npm install bellhop-iframe

Importing Bellhop

The Bellhop module contains support for ES6 modules, CommonJS and browser global definitions. To import with ES6,

import { Bellhop } from 'bellhop-iframe';

To import with CommonJS, refer instead to the UMD build

const Bellhop = require('bellhop-iframe/dist/bellhop-umd.js');

You can also import the UMD version by using import

import 'bellhop-iframe/dist/bellhop-umd.js';

Lastly, the UMD module can also be directly included on an HTML page. This will declare Bellhop and attach it directly to window

<script src="node_modules/bellhop-iframe/dist/bellhop-umd.js"></script>

Basic Usage

Here's a very simple example to get started. We have two pages index.html and child.html.This is the minimum you need to start get them talking to each other.

Contents of index.html

<iframe src="child.html" id="page" width="200" height="200"></iframe>
<script>

// Create the bellhop object
const bellhop = new Bellhop();

// Pass in the iframe DOM object
bellhop.connect(document.getElementById("page"));

// Listen for the 'init' event from the iframe
bellhop.on('init', function(event){
  // Handle the event here!
});

// Send data to the iframe
bellhop.send('user', {
  "name" : "Dave Smith",
  "age" : 16,
  "city" : "Boston"
});
</script>

Contents of child.html

<script>

// Create the bellhop object
const bellhop = new Bellhop();
bellhop.connect();

// An example event to sent to the parent
bellhop.send('init');

// Handle events from the parent
bellhop.on('user', function(event){
  // Capture the data from the event
  const user = event.data;
}
</script>

Available Methods

new Bellhop

The constructor creates a new Bellhop instance, taking an optional unique identifier for this instance. If no id is provided, a random one is selected

connect

Connects a Bellhop instance to an iframe, or it's containing window. For instance, given a Bellhop instance bellhop:

bellhop.connect();

will connect a child iframe to it's parent, allowing it to emit messages out of the iframe. However,

var iframe = document.querySelector('iframe');
bellhop.connect(iframe);

allows a containing page to connect with an interior iframe and emit message into the iframe.

destroy

disconnect removes any listener for events from another frame, and stops listening for messages altogether

send

Sends a named message to another iframe:

bellhop.send('newHighscore', { value: 100 });

fetch and respond

Convenience methods for automating response of values between the interior and exterior of frames. For instance:

// parent.html
var iframe = document.querySelector('iframe');
var bellhop = new Bellhop(iframe);
bellhop.connect();
bellhop.respond('config', { difficulty: 'hard', theme: 'dark' });


// child.html
var bellhop = new Bellhop();
bellhop.connect();
bellhop.fetch('config', function(result) {
  console.log(result); // { difficulty: 'hard', theme: 'dark' }
});

target

Property for retrieving the iframe element through which this Bellhop instance is communicating:

var iframe = document.querySelector('iframe');
var bellhop = new Bellhop(iframe);

console.log(bellhop.target === iframe.contentWindow); // true

License

Copyright (c) 2018 Springroll

Released under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.