As for now, standard HTML5 Web Storage (a.k.a Local Storage) doesn't now allow cross domain data sharing. This may be a big problem in an organization which have a lot of sub domains and wants to share client data between them.
xdLocalStorage is a lightweight js library which implements LocalStorage interface and support cross domain storage by using iframe post message communication.
Although cookies can be shared between sub domains, cookies have the overhead of being sent to the server on each request. They're also have a size limit (4K for all cookies together)
Download latest release from here or use bower (recommended)
bower install xdLocalStorage --save
- Create an empty html with the following content:
<!DOCTYPE html>
<html>
<head>
<script src="xdLocalStoragePostMessageApi.min.js"></script>
</head>
<body>
This is the magical iframe
</body>
</html>
- On your client page (the page you will read/store your data from) add:
<!-- if you use angular continue reading.. there's angular support -->
<script src="scripts/xdLocalStorage.min.js"></script>
- Init xdLocalStorage
xdLocalStorage.init(
{
/* required */
iframeUrl:'path to your html from step 1',
//an option function to be called right after the iframe was loaded and ready for action
initCallback: function () {
console.log('Got iframe ready');
}
}
);
// Store
xdLocalStorage.setItem(key, value, function (data) { /* callback */ });
// Retrieve
xdLocalStorage.getItem(key, function (data) { /* callback */ });
// Remove
xdLocalStorage.removeItem(key, function (data) { /* callback */ });
// Key Name
xdLocalStorage.key(index, function (data) { /* callback */ });
// Clear All
xdLocalStorage.clear(function (data) { /* callback */ });
-
import
ng-xdLocalStorage.min.js
instead ofxdLocalStorage.min.js
-
include xdLocalStorage module and config it.
angular.module('yourModule', ['xdLocalStorage']).config(function (xdLocalStorageProvider) {
xdLocalStorageProvider.init(
{
/* required */
iframeUrl:'path to your html from step 1',
//an option function to be called right after the iframe was loaded and ready for action
initCallback: function () {
console.log('Got iframe ready');
}
});
});
- inject xdLocalStorage where ever you want and use the API
angular.module('yourModule').run(function(xdLocalStorage) {
xdLocalStorage.setItem(key, value, function (data) { /* callback */});
});
Here's the demo
I covered the basic API with tests here