Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
moved xdomain_only option to constructor and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ecaroth committed Jun 1, 2016
1 parent ab81d78 commit ea1e8ad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ Simply include the script on any page where it's needed, create a new instance o
### API
##### xDomainIframe( iframe_domain, namespace )
##### xDomainIframe( iframe_domain, namespace, xdomain_only )
Create a new instance of the xDomainIframe object that creates the iframe in the page and is ready for usage
`iframe_domain` (string, required) the domain, and optional path, where the iframe html script should be loaded from - NOTE should match the protocol/host/port of where the JS script is loaded from
`namespace` (string,optional) a namespace to use for postMessage passing - prevents collission if you are running multiple instances of this lib on the page... usually not needed
`xdomain_only` (boolean, optional, default false) if the cookie should _only_ be set on the xdomain site, not locally.. meaning that the xdomain version acts as the source of truth for the cookie value and eliminates local caching
#####.set( cookie_name, cookie_value, expires_days, xdomain_only )
#####.set( cookie_name, cookie_value, expires_days )
Set the value of the xdomain (& local) cookie
`cookie_name` (string, required) the name of the cookie (both for local domain & iframe domain)
Expand All @@ -43,10 +45,8 @@ Set the value of the xdomain (& local) cookie
`expires_days` (int, optional) # of days to use for setting cookie expiration (default is 30)
`xdomain_only` (boolean, optional, default false) if the cookie should _only_ be set on the xdomain site, not locally.. meaning that the xdomain version acts as the source of truth for the cookie value and eliminates local caching
#####.get( cookie_name, callback, expires_days, xdomain_only )
#####.get( cookie_name, callback, expires_days )
Get the value of the xdomain (& local) cookie with complete callback. _NOTE: this function also re-ups the xdomain cookie as if it was being re-set with .set()_
`cookie_name` (string, required) the name of the cookie (both for local domain & iframe domain)
Expand All @@ -55,9 +55,6 @@ Get the value of the xdomain (& local) cookie with complete callback. _NOTE: thi
`expires_days` (int, optional) # of days to use for setting/re-upping cookie expiration (default is 30)
`xdomain_only` (boolean, optional, default false) if cookie is xdomain only - see documentation for .set()
### Testing
There's a full test suite that leverages zombie/connect to mock & test the library behavior across multiple domains in multiple different situations. There is also a pre-build development setup to load/test in local environments in the library. Both of these rely on npm packages, so be sure to do an `npm install` in the root dir before running.
Expand Down
15 changes: 8 additions & 7 deletions src/xdomain_cookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(exports) {
"use strict";

var xDomainCookie = function( iframe_path, namespace, iframe_load_timeout_ms ){
var xDomainCookie = function( iframe_path, namespace, xdomain_only, iframe_load_timeout_ms ){
//iframe_path = full TLD (and optional path) to location where iframe_shared_cookie.html is served from, and domain cookie will be set on
//namespace = namespace to use when identifying that postMessage calls incoming are for our use

Expand All @@ -14,7 +14,8 @@
_callbacks = [], //list of pending callbacks to ping when iframe is ready or err occurs
_xdomain_cookie_data = {}, //shared cookie data set by the iframe after load/ready
_id = new Date().getTime(), //identifier to use for iframe in case there are multiple on the page
_default_expires_days = 30; //default expiration days for cookies when re-uppded
_default_expires_days = 30, //default expiration days for cookies when re-uppded
_xdomain_only = xdomain_only===true; //should we ONLY use xdomain cookies (and avoid local cache)

//function called on inbound post message - filter/verify that message is for our consumption, then set ready data an fire callbacks
function _inbound_postmessage( event ){
Expand Down Expand Up @@ -90,20 +91,20 @@
}

//function to set the value for both cookies (local & xdomain)
function _set_xdomain_cookie_value( cookie_name, cookie_value, expires_days, xdomain_only ){
function _set_xdomain_cookie_value( cookie_name, cookie_value, expires_days ){

//if iframe isn't ready, wait for it to be ready
if(!_iframe_ready && !_iframe_load_error){
return _callbacks.push(function(load_error){
_set_xdomain_cookie_value( cookie_name, cookie_value, expires_days, xdomain_only);
_set_xdomain_cookie_value( cookie_name, cookie_value, expires_days);
});
}

expires_days = expires_days || _default_expires_days;
//if cookie is empty (null or undefined) delete the cookie
expires_days = (cookie_value===null || cookie_value===undefined) ? -100 : expires_days;

if(xdomain_only!==true) _set_local_cookie( cookie_name, cookie_value, expires_days );
if(!_xdomain_only) _set_local_cookie( cookie_name, cookie_value, expires_days );

if(!_iframe_load_error){
_set_cookie_in_iframe( cookie_name, cookie_value, expires_days );
Expand All @@ -113,15 +114,15 @@

//function to call after instantiation to sync a cookie, supplying a cookie name, value to write if it does NOT exist, expires
//time (in ms from now), and a callback for completion (which includes the resolved cookie value as the only argument)
function _get_xdomain_cookie_value( cookie_name, callback, expires_days, xdomain_only ){
function _get_xdomain_cookie_value( cookie_name, callback, expires_days ){

expires_days = expires_days || _default_expires_days;

//cb function to create closure for pending user callback
function _cb( xdomain_success, cookie_val, callback ){

//re-up the cookie
_set_xdomain_cookie_value( cookie_name, cookie_val, expires_days, xdomain_only );
_set_xdomain_cookie_value( cookie_name, cookie_val, expires_days );

if(typeof callback == 'function') callback( cookie_val );
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
is_dev = true;
}

var xd_cookie = xDomainCookie( iframe_url, 'test.xdsc' );
var xd_cookie = xDomainCookie( iframe_url, 'test.xdsc', xdomain_only );

xd_cookie.get( 'test_cookie', function( cookie_val ){

Expand Down

0 comments on commit ea1e8ad

Please sign in to comment.