<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zeta-Puppis.com &#187; Javascript</title>
	<atom:link href="http://zeta-puppis.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://zeta-puppis.com</link>
	<description>my very own personal corner</description>
	<lastBuildDate>Wed, 07 Apr 2010 15:36:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript script execution in innerHTML: another round</title>
		<link>http://zeta-puppis.com/2007/05/27/javascript-script-execution-in-innerhtml-another-round/</link>
		<comments>http://zeta-puppis.com/2007/05/27/javascript-script-execution-in-innerhtml-another-round/#comments</comments>
		<pubDate>Sun, 27 May 2007 15:20:32 +0000</pubDate>
		<dc:creator>kratorius</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://kratorius.simosnap.net/2007/05/27/javascript-script-execution-in-innerhtml-another-round/</guid>
		<description><![CDATA[More than one year ago I was playing with AJAX, and I was facing a problem with scripts contained in documents loaded through XMLHttpRequest. So, at that time, I wrote two blog posts talking about this issue. The first was just a modify to the well known AHAH technique, while the second post was a [...]]]></description>
			<content:encoded><![CDATA[<p>More than one year ago I was playing with AJAX, and I was facing a problem with scripts contained in documents loaded through XMLHttpRequest. So, at that time, I wrote two blog posts talking about this issue. The first was just a modify to the well known <a href="http://microformats.org/wiki/rest/ahah">AHAH</a> technique, while the second post was a script I entirely wrote by myself: &#8220;<a href="http://kratcode.wordpress.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/">Javascript script execution in innerHTML: the&nbsp;revenge</a>&#8221;.</p>
<p>Now more than a year has passed and technologies are evolved. Now the web is full of very powerful AJAX frameworks and much probably, for medium/big projects you won&#8217;t need this kind of &#8220;hack&#8221; anymore. But there are few developers across the world that still hand-code their little ajax tricks and needs this. So, since I received a lot of comments about that, I&#8217;m writing here again to update you about the modifies that have been done to that&nbsp;script.</p>
<p>That script suffered of a (relatively) big problem: if you had a document.write() call in the external script you loaded, well, it won&#8217;t work. Jeremy Bell has modified that script in order to have this functionality included. You can see it working at&nbsp;<a href="http://www.blackoutwebdesign.com/ajax.demo.php">http://www.blackoutwebdesign.com/ajax.demo.php</a>.</p>
<p>For other discussion about the topic, look at the comments in the post, they have been very helpful to me to correct various compatibility&nbsp;issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://zeta-puppis.com/2007/05/27/javascript-script-execution-in-innerhtml-another-round/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript script execution in innerHTML: the revenge</title>
		<link>http://zeta-puppis.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/</link>
		<comments>http://zeta-puppis.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/#comments</comments>
		<pubDate>Tue, 07 Mar 2006 18:09:00 +0000</pubDate>
		<dc:creator>kratorius</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kratorius.simosnap.net/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/</guid>
		<description><![CDATA[The execJS() I posted some time ago have some problems (and it, yes, was just a modify to the AHAH version). I didn&#8217;t really realized what exactly it is, but I found a simple (?) solution. The problem, as far as I can understand, is that eval() doesn&#8217;t always execute the code. So here it [...]]]></description>
			<content:encoded><![CDATA[<p>The <code>execJS()</code> I posted <a href="http://kratcode.wordpress.com/2006/02/23/javascript-script-execution-in-innerhtml/">some time ago</a> have some problems (and it, yes, was just a modify to the AHAH version). I didn&#8217;t really realized what exactly it is, but I found a simple (?) solution. The problem, as far as I can understand, is that <code>eval()</code> doesn&#8217;t always execute the code. So here it is the workaround: look for <code>&amp;lt;script&amp;gt;</code> tags, take its content and create a new element into the <code>&amp;lt;head&amp;gt;</code> with <code>createElement/appendChild</code>. In this way we should also be more standard-compliant than&nbsp;before:</p>
<pre><code>function execJS(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&amp;lt;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);
    }
  }
};</code></pre>
<p>I tested it only under firefox, but it should work on other browsers too. If it doesn&#8217;t, let me&nbsp;know.</p>
<p><strong>Update (October 1st, 2006)</strong>: now it works under Internet Explorer too (if you want to inject some text in a script element in IE you can&#8217;t use .innerHTML, but you have to use&nbsp;.text!)</p>
]]></content:encoded>
			<wfw:commentRss>http://zeta-puppis.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Javascript script execution in innerHTML</title>
		<link>http://zeta-puppis.com/2006/02/23/javascript-script-execution-in-innerhtml/</link>
		<comments>http://zeta-puppis.com/2006/02/23/javascript-script-execution-in-innerhtml/#comments</comments>
		<pubDate>Thu, 23 Feb 2006 19:52:00 +0000</pubDate>
		<dc:creator>kratorius</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kratorius.simosnap.net/2006/02/23/javascript-script-execution-in-innerhtml/</guid>
		<description><![CDATA[I am developing a new website for a club I&#8217;m founding. I can&#8217;t tell you what this club will do (also because I don&#8217;t really know, yet) but the site is going to become a challenge.
I wanted to use all the latest, innovative technologies around here. So, I just took PHP, Javascript, XHTML (strictly 1.0 [...]]]></description>
			<content:encoded><![CDATA[<p>I am developing a new website for a club I&#8217;m founding. I can&#8217;t tell you what this club will do (also because I don&#8217;t really know, yet) but the site is going to become a challenge.<br />
I wanted to use all the latest, innovative technologies around here. So, I just took PHP, Javascript, XHTML (strictly 1.0 strict!) and a MySQL database in my gunnysack and gone around with&nbsp;them.</p>
<p>There&#8217;s an issue I&#8217;ve been facing against that just got me crazy: if I include a HTML page (not really, a php generated one to be precise) through innerHTML the content that was into the <code>&amp;lt;script&amp;gt;...&amp;lt;/script&amp;gt;</code> tags was not executed. After some google-ing, I found that this is a security implementation: browsers doesn&#8217;t allow code to be executed into a innerHTML block. If you break a moment and think about it, it makes perfectly sense, but I need of such a feature so I had to found a&nbsp;workaround.</p>
<p>The idea was this: let threat the html page as an xml file (although it is really one), so let use the <acronym title="Document Object Model">DOM</acronym> to browse it looking for <code>&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;</code>, then pass its content to <code>eval()</code>.<br />
I was not using any AJAX library, so I had to wrote all the code by myself. Here is a&nbsp;snippet:</p>
<pre><code>function execJS(node) {
       var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
       var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
       var bMoz = (navigator.appName == 'Netscape');

var st = node.getElementsByTagName('SCRIPT');
       var strExec;

for(var i=0;i&amp;lt;st.length; i++) {
               if (bSaf) {
                       strExec = st[i].innerHTML;
               } else if (bOpera) {
                       strExec = st[i].text;
               } else if (bMoz) {
                       strExec = st[i].textContent;
               } else {
                       strExec = st[i].text;
               }

try {
                       eval(strExec);
               } catch(e) {
                       alert(e);
               }
       }
}

function handlePageChanging()
{
       if (http.readyState == 4) {
               var cnt_big = document.getElementById('cnt_big');
               cnt_big.innerHTML = http.responseText;

execJS(cnt_big);
               hideLoading();
               doLinks();
       }
}

function changeContentPage(param)
{
       _current = param;

var url = param + '&amp;amp;aJ=y';
       showLoading();
       http.open("GET", url, true);
       http.onreadystatechange = handlePageChanging;
       http.send(null);
}</code></pre>
<p>Of course you can&#8217;t copy and paste this code except the <code>execJS</code> function (since it is this post&#8217;s purpose) but the important thing is that you notice how it works. Pratically, the page makes an AJAX request with <code>changeContentPage()</code>, then handle it through <code>handlePageChanging()</code>. So when we&#8217;re going to set up the content with innerHTML, we call <code>execJS()</code> so it will eval()-ize all the <code>&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;</code>&nbsp;contents.</p>
<p><b>Update</b>: see this&nbsp;<a href="http://kratcode.blogspot.com/2006/03/javascript-script-execution-in.html">http://kratcode.blogspot.com/2006/03/javascript-script-execution-in.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zeta-puppis.com/2006/02/23/javascript-script-execution-in-innerhtml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
