Passing Parameters to an OpenSocial Gadget Hosted in Regular HTML

Gadget parameters are UserPref’s in the gadget specification. When a user adds a gadget to an OpenSocial container they can set these when they add the gadget to the container or later once the gadget is in place using the “settings” link. We have to reproduce this in our plain HTML container by taking parameters passed to the script and setting UserPrefs directly through cookie modification.

On the web page the script is included like:


Added to this file is:

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      //val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;	
}

function setUserPrefs(term, course) {
	document.cookie = "gadgetUserPrefs-0=term=" + term + "&course=" + 
course + "; expires=-1; path=/test/CourseResourcesGadget/; domain=sandbox.library.ucla.edu"; 
}

var scripts = document.getElementsByTagName('script');
var myScript = scripts[0];
var queryString = myScript.src.replace(/^[^\?]+\??/,'');
var params = parseQuery( queryString );
setUserPrefs(params.term, params.course);

This new section parses the parameters passed to the script and sets the cookie that relates to UserPrefs.