Giter VIP home page Giter VIP logo

stereojs's Introduction

StereoJS

About

Stereo.js is a little library for communication between browser's tabs.

Example

You can open example/frame1.html and example/frame2.html, then type some text in input of frame1.html, see the change of frame2.html at the same time.

NOTE: You need a server to make it work.

Technical details

Using localStorage Events mechanism.

Usage

stereo.broadcast(EventName:String, Data:Object)

This API is used to tell other tabs that something has occured in this tab, and you can send data to other tabs at the same time.

// frame1.html
stereo.broadcast('todo', {
	content: 'To complete a Hello World program'
})  

Tabs except this tab can catch this event by using stereo.on API.

NOTE: Data must be a plain JavaScript Object, and it doesn't accept function as value. Function context in one tab will not be transported to other tabs.

stereo.on(EventName:String, callback:Function)

Listen to other page's changes. When other broadcast the event called EventName, then the callback function in this page will be executed. Data added while other tabs broadcast event will be passed into callback function. In other words, you can get data from other tabs in callback function.

// frame2.html
stereo.on('todo', function (todo) {
	document.getElementById('heading2').innerHTML = todo.content // 'To complete a Hello World program'
})

As shown above. Frame1 uses stereo.broadcast to trigger an event named todo, and addon an object data. Frame2 listens to todo event by using stereo.on API. When frame1 broadcasts a todo event, callback function in Frame2 will be executed, and you can get data sent by frame1 in callback function.

stereo.off(EventName:String, callback:Function)

The opposite of stereo.on. You may want to stop listening to certain event, you can use this API.

function addTodo() {
	document.getElementById('heading2').innerHTML = todo.content
}

stereo.on('todo', addTodo)

// Something happens

stereo.off('todo', addTodo)

Compatibility

  • IE 8+
  • Firefox 3.0+
  • Chrome 4.0+
  • Other Browsers that support localStorage events

License

MIT

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.