[pLog-svn] r5736 - plog/trunk/js/ui

oscar at devel.lifetype.net oscar at devel.lifetype.net
Fri Jul 27 15:54:13 EDT 2007


Author: oscar
Date: 2007-07-27 15:54:12 -0400 (Fri, 27 Jul 2007)
New Revision: 5736

Modified:
   plog/trunk/js/ui/contentoverlay.js
   plog/trunk/js/ui/core.js
Log:
Added event Lifetype.UI.ContentOverlay.Events.dataLoaded to inform other javascript code that the data for the overlay was successfully loaded.
Added the Lifetype.execJS method that allows to execute any javascript code within a node. This is used by the ContentOverlay class to execute any javascript code that was fetched by XmlHttpRequest and assigned to the innerHTML property of a node. Otherwise the browser, for security reasons, will not do it.


Modified: plog/trunk/js/ui/contentoverlay.js
===================================================================
--- plog/trunk/js/ui/contentoverlay.js	2007-07-26 21:50:46 UTC (rev 5735)
+++ plog/trunk/js/ui/contentoverlay.js	2007-07-27 19:54:12 UTC (rev 5736)
@@ -30,6 +30,16 @@
 }
 YAHOO.extend( Lifetype.UI.ContentOverlay, YAHOO.widget.Panel );
 
+/**
+ * Namespace for events generated by the ContentOverlay class
+ */
+Lifetype.UI.ContentOverlay.Events = function() {}
+
+/**
+ * CustomEvent object fired when the data for the overlay has been loaded.
+ */
+Lifetype.UI.ContentOverlay.Events.dataLoaded = new YAHOO.util.CustomEvent('dataLoaded');
+
 Lifetype.UI.ContentOverlay.prototype.hide = function()
 {
 	// this is doing nothing now, but I am planning to perform a check here 
@@ -41,8 +51,16 @@
 Lifetype.UI.ContentOverlay.prototype.show = function()
 {
 	c = YAHOO.util.Connect.asyncRequest( 'GET', this.url, { scope:this, success: function(o) {
-			this.setBody( o.responseText );
+			this.setBody( o.responseText );			
 			this.center();
+			
+			// this hack allows us to execute any javascript code that has been fetched via
+			// XmlHttpRequest and assigned to a node via innerHTML
+			s = document.createElement( 'span' );
+			s.innerHTML = o.responseText;
+			Lifetype.execJS( s );
+			
+			Lifetype.UI.ContentOverlay.Events.dataLoaded.fire( o );			
 		}
 	});
 	this.render( document.body );

Modified: plog/trunk/js/ui/core.js
===================================================================
--- plog/trunk/js/ui/core.js	2007-07-26 21:50:46 UTC (rev 5735)
+++ plog/trunk/js/ui/core.js	2007-07-27 19:54:12 UTC (rev 5736)
@@ -98,6 +98,62 @@
 }
 
 /**
+ * When loading Javascript code and assigning it to a node via
+ * innerHTML, it won't be executed by the browser for apparently
+ * security reasons. This method will take a given node, look for
+ * <script> tags and execute them accordingly.
+ *
+ * Shamelessly copied from here: 
+ * http://kratcode.wordpress.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/
+ *
+ * @param node A starting node where to look for script
+ * tags.
+ */
+Lifetype.execJS = function(node)
+{
+  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
+  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
+  var bMoz = (navigator.appName == 'Netscape');
+
+  if (!node) return;
+
+  /* IE wants it uppercase */
+  var st = node.getElementsByTagName('SCRIPT');
+  var strExec;
+
+  for(var i=0;i<st.length; i++)
+  {
+    if (bSaf) {
+      strExec = st[i].innerHTML;
+      st[i].innerHTML = "";
+    } else if (bOpera) {
+      strExec = st[i].text;
+      st[i].text = "";
+    } else if (bMoz) {
+      strExec = st[i].textContent;
+      st[i].textContent = "";
+    } else {
+      strExec = st[i].text;
+      st[i].text = "";
+    }
+
+    try {
+      var x = document.createElement("script");
+      x.type = "text/javascript";
+
+      /* In IE we must use .text! */
+      if ((bSaf) || (bOpera) || (bMoz))
+        x.innerHTML = strExec;
+      else x.text = strExec;
+
+      document.getElementsByTagName("head")[0].appendChild(x);
+    } catch(e) {
+      alert(e);
+    }
+  }
+}
+
+/**
  * JSon related code
  */
 Lifetype.JSon = function() {}



More information about the pLog-svn mailing list