gitmyhub

easyXDM

JavaScript ★ 0 updated 15y ago ⑂ fork

A javascript library providing cross-browser, cross-site messaging/method invocation.

What easyXDM Does

easyXDM solves a fundamental problem on the web: it lets you send messages and call functions between two separate web pages that live on different domains. Normally, browsers block this kind of cross-domain communication for security reasons. This library works around that limitation, making it possible for a main page to safely talk to code running inside an embedded iframe (a mini-window) on a different domain.

How It Works

The library provides two main tools. The first is a simple messaging system called Socket, which lets you send text messages back and forth between domains. The second is called Rpc, which is fancier—it lets you expose actual JavaScript functions on one side and call them like method stubs on the other, with automatic handling of responses and errors.

Behind the scenes, easyXDM picks the best communication method available in your browser. Modern browsers like Chrome, Firefox, and Safari use the browser's built-in postMessage API, which is fast and direct. For older browsers like Internet Explorer 6 and 7, it falls back to Flash-based communication. For browsers that don't support any of these, it uses a slower method based on URL hashes. The library handles all these differences automatically—you just write the same code and it works everywhere.

Who Would Use This

This is useful if you're building a service that needs to run inside other people's websites. For example, imagine you run an analytics company and want to let customers embed tracking code on their site. The customer's page lives on their domain, but your tracking code is embedded in an iframe from your domain. With easyXDM, they can securely communicate. Another example: a comment widget, payment form, or any third-party embed that needs to send data back to its home server or receive instructions from the hosting page. The README also includes a built-in CORS (cross-domain request) helper, so you can make AJAX calls to different domains through this bridge.