<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The Magic Eval</title>
	<atom:link href="http://nerd.metrocat.org/2005/09/the-magic-eval/feed" rel="self" type="application/rss+xml" />
	<link>http://nerd.metrocat.org/2005/09/the-magic-eval</link>
	<description>You're reading Jeff Watkins' thoughts about Web application design and development with a little bit of technology ranting thrown in for free.</description>
	<lastBuildDate>Sun, 08 Jun 2008 07:06:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: natasha kuzmanoska</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-484</link>
		<dc:creator>natasha kuzmanoska</dc:creator>
		<pubDate>Sat, 29 Jul 2006 14:49:02 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-484</guid>
		<description>&lt;p&gt;for firefox  you can use window._content.eval() if you want to get global context&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>for firefox  you can use window._content.eval() if you want to get global context</p>]]></content:encoded>
	</item>
	<item>
		<title>By: natasha kuzmanoska</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-482</link>
		<dc:creator>natasha kuzmanoska</dc:creator>
		<pubDate>Sat, 29 Jul 2006 13:35:30 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-482</guid>
		<description>&lt;p&gt;If you change the way you are declaring functions you can acces them globaly without problem, for the global variables just do not use var;   &lt;/p&gt;

&lt;p&gt;function go(){
  eval(â€bar = function(){return â€˜barâ€™}â€);
  eval(â€b = 22;â€);
  eval(â€var a = 22;â€); //for this context only 
}&lt;/p&gt;

&lt;p&gt;function go1(){
 var b = 33; // for this context
}
go();
go1();&lt;/p&gt;

&lt;p&gt;alert(bar());
alert(b); Ok
alert(a); does not work&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>If you change the way you are declaring functions you can acces them globaly without problem, for the global variables just do not use var;   </p>

<p>function go(){
  eval(â€bar = function(){return â€˜barâ€™}â€);
  eval(â€b = 22;â€);
  eval(â€var a = 22;â€); //for this context only 
}</p>

<p>function go1(){
 var b = 33; // for this context
}
go();
go1();</p>

<p>alert(bar());
alert(b); Ok
alert(a); does not work</p>]]></content:encoded>
	</item>
	<item>
		<title>By: raulshred</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-467</link>
		<dc:creator>raulshred</dc:creator>
		<pubDate>Fri, 21 Jul 2006 07:30:26 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-467</guid>
		<description>&lt;p&gt;This last method don&#039;t rules in Konqueror :(&lt;/p&gt;

&lt;p&gt;if(MSIE)
{
  window.execScript(sFunction,&#039;javascript&#039;);
}
  else
{
  setTimeout(&#039;eval(&#039;&#039;+sFunction.replace(/&#039;/g,&#039;&#039;&#039;)+&#039;&#039;)&#039;,0);
}&lt;/p&gt;

&lt;p&gt;This code rules in MSIE,Gecko,Opera,Safari,Konqueror.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This last method don&#8217;t rules in Konqueror :(</p>

<p>if(MSIE)
{
  window.execScript(sFunction,&#8217;javascript&#8217;);
}
  else
{
  setTimeout(&#8217;eval(&#8221;+sFunction.replace(/&#8217;/g,&#8221;&#8217;)+&#8221;)&#8217;,0);
}</p>

<p>This code rules in MSIE,Gecko,Opera,Safari,Konqueror.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Watkins</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-437</link>
		<dc:creator>Jeff Watkins</dc:creator>
		<pubDate>Wed, 05 Jul 2006 22:21:06 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-437</guid>
		<description>&lt;p&gt;Eric, that&#039;s a good idea. I&#039;m not certain why that didn&#039;t occur to me. It&#039;s great to be able to have a complete solution, even for MSIE.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Eric, that&#8217;s a good idea. I&#8217;m not certain why that didn&#8217;t occur to me. It&#8217;s great to be able to have a complete solution, even for MSIE.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Davis</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-435</link>
		<dc:creator>Eric Davis</dc:creator>
		<pubDate>Wed, 05 Jul 2006 20:05:39 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-435</guid>
		<description>&lt;p&gt;I was just struggling with IE not loading eval&#039;ed Javascript into the global space and I didn&#039;t know about the execScript IE function.  Thanks!  I ended up with the following code which works on all the browsers I tried:&lt;/p&gt;

&lt;p&gt;if (window.ActiveXObject)
{
    window.execScript(jsCode);
}
else /* (window.XMLHttpRequest) */
{
    window.eval(jsCode);
}&lt;/p&gt;

&lt;p&gt;Checking for ActiveXObject is analogous to most of the AJAX getHttpRequest implementations I&#039;ve seen.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I was just struggling with IE not loading eval&#8217;ed Javascript into the global space and I didn&#8217;t know about the execScript IE function.  Thanks!  I ended up with the following code which works on all the browsers I tried:</p>

<p>if (window.ActiveXObject)
{
    window.execScript(jsCode);
}
else /* (window.XMLHttpRequest) */
{
    window.eval(jsCode);
}</p>

<p>Checking for ActiveXObject is analogous to most of the AJAX getHttpRequest implementations I&#8217;ve seen.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: bot</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-415</link>
		<dc:creator>bot</dc:creator>
		<pubDate>Mon, 15 May 2006 14:11:43 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-415</guid>
		<description>&lt;p&gt;Brandon &amp; Jeff, thanks for the trick. However, it did not work directly for me. I had to do several changes:&lt;/p&gt;

&lt;p&gt;1/ simplify a lot ajax_getscripts(), that finally looks like:&lt;/p&gt;

&lt;p&gt;function ajax_getscripts(indoc) {
    var outscripts = [];
    var inelems = indoc.getElementsByTagName(&#039;script&#039;);
    for(var i = 0; i &lt; inelems.length; i++) {
        outscripts.push(inelems[i].innerHTML);
    }
    return outscripts;
}
In fact, I don&#039;t really understand why Brandon&#039;s is so complicated (might be related to the point 2/ below, though) &lt;/p&gt;

&lt;p&gt;2/ modified the way we send the data to it. Brandon wrote&lt;/p&gt;

&lt;p&gt;var ajscripts = ajax_getscripts(xhttp.responseXML);&lt;/p&gt;

&lt;p&gt;which evaluates to an empty string under IE 6 (maybe others, too) so I had to first assign the whole HTML block to the DIV I want to fill, and then pass this DIV to ajax_getscripts.&lt;/p&gt;

&lt;p&gt;3/ The functions in my  tag were still not present in the global context in IE, but were in Firefox. The code was executed correctly (validated with printf()s -- sorry, alert()s)  but would not go into the damn global context. Thus, I created a singleton&lt;/p&gt;

&lt;p&gt;g_functionCatalog = new Object() // created in the global scope, only once (never gets resent through ajax async requests)&lt;/p&gt;

&lt;p&gt;and then I bound all the functions returned by the further XmlHttpRequests to that object, e.g.&lt;/p&gt;

&lt;p&gt;g_functionCatalog.myFunctionReturnedByAjax = function() { [...] }&lt;/p&gt;

&lt;p&gt;This works. The drawback is that I have to call those functions with the syntax functionCatalog.myFunctionReturnedByAjax() instead of simply myFunctionReturnedByAjax().&lt;/p&gt;

&lt;p&gt;Anyway, thanks a lot for the info. I had been looking for 2-3 hours how to promote a javascript function to the global scope; your site has just given me the precise answer.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Brandon &amp; Jeff, thanks for the trick. However, it did not work directly for me. I had to do several changes:</p>

<p>1/ simplify a lot ajax_getscripts(), that finally looks like:</p>

<p>function ajax_getscripts(indoc) {
    var outscripts = [];
    var inelems = indoc.getElementsByTagName(&#8217;script&#8217;);
    for(var i = 0; i &lt; inelems.length; i++) {
        outscripts.push(inelems[i].innerHTML);
    }
    return outscripts;
}
In fact, I don&#8217;t really understand why Brandon&#8217;s is so complicated (might be related to the point 2/ below, though) </p>

<p>2/ modified the way we send the data to it. Brandon wrote</p>

<p>var ajscripts = ajax_getscripts(xhttp.responseXML);</p>

<p>which evaluates to an empty string under IE 6 (maybe others, too) so I had to first assign the whole HTML block to the DIV I want to fill, and then pass this DIV to ajax_getscripts.</p>

<p>3/ The functions in my  tag were still not present in the global context in IE, but were in Firefox. The code was executed correctly (validated with printf()s &#8212; sorry, alert()s)  but would not go into the damn global context. Thus, I created a singleton</p>

<p>g_functionCatalog = new Object() // created in the global scope, only once (never gets resent through ajax async requests)</p>

<p>and then I bound all the functions returned by the further XmlHttpRequests to that object, e.g.</p>

<p>g_functionCatalog.myFunctionReturnedByAjax = function() { [...] }</p>

<p>This works. The drawback is that I have to call those functions with the syntax functionCatalog.myFunctionReturnedByAjax() instead of simply myFunctionReturnedByAjax().</p>

<p>Anyway, thanks a lot for the info. I had been looking for 2-3 hours how to promote a javascript function to the global scope; your site has just given me the precise answer.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Watkins</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-218</link>
		<dc:creator>Jeff Watkins</dc:creator>
		<pubDate>Fri, 10 Mar 2006 20:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-218</guid>
		<description>&lt;p&gt;Mat, unfortunately, your code only works in Internet Explorer. Brandon&#039;s suggestion works in all browsers, which is really important for almost any credible Javascript library.&lt;/p&gt;

&lt;p&gt;It can be really easy to fall into the trap of using Microsoft-only features. For example, &lt;code&gt;element.children&lt;/code&gt; is the MS-only version of &lt;code&gt;element.childNodes&lt;/code&gt;. It returns only elements instead of elements and text nodes, and is a little more convenient than &lt;code&gt;childNodes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately, &lt;code&gt;children&lt;/code&gt; isn&#039;t supported under Mozilla. So you either have to code things twice (not a good idea in my book) or code to the standard. Of course, bugs in any browser often require a second or possibly more code paths.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Mat, unfortunately, your code only works in Internet Explorer. Brandon&#8217;s suggestion works in all browsers, which is really important for almost any credible Javascript library.</p>

<p>It can be really easy to fall into the trap of using Microsoft-only features. For example, <code>element.children</code> is the MS-only version of <code>element.childNodes</code>. It returns only elements instead of elements and text nodes, and is a little more convenient than <code>childNodes</code>.</p>

<p>Unfortunately, <code>children</code> isn&#8217;t supported under Mozilla. So you either have to code things twice (not a good idea in my book) or code to the standard. Of course, bugs in any browser often require a second or possibly more code paths.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Mat FrÃ©dÃ©ric</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-215</link>
		<dc:creator>Mat FrÃ©dÃ©ric</dc:creator>
		<pubDate>Fri, 10 Mar 2006 13:01:28 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-215</guid>
		<description>&lt;p&gt;you should try this function which gives access to the global scope in internet explorer:&lt;/p&gt;

&lt;p&gt;window.execScript(content,&quot;javascript&quot;);&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>you should try this function which gives access to the global scope in internet explorer:</p>

<p>window.execScript(content,&#8221;javascript&#8221;);</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Black</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-21</link>
		<dc:creator>Brandon Black</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-21</guid>
		<description>&lt;p&gt;I&#039;ve been solving the same problem recently, and it can be done even simpler.  setTimeout() itself evals a string argument, therefore there&#039;s no need for the escaping of single-quotes via replace() or the extra layer of eval, just do setTimeout(codestring,0); where codestring is a variable containing the js code from the  tags.  Here was the complete thing I used in my ajax library:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Strip  elements out of an XML document retreived via xmlHttpRequest,
// returning them as an array of strings

function ajax_getscripts(indoc) {
  var outscripts = [];

  var inelems = indoc.getElementsByTagName(&#039;script&#039;);
  for(var i = 0; i I&#039;ve been solving the same problem recently, and it can be done even simpler.  setTimeout() itself evals a string argument, therefore there&#039;s no need for the escaping of single-quotes via replace() or the extra layer of eval, just do setTimeout(codestring,0); where codestring is a variable containing the js code from the  tags.  Here was the complete thing I used in my ajax library:

// Strip  elements out of an XML document retreived via xmlHttpRequest,
// returning them as an array of strings

function ajax_getscripts(indoc) {
  var outscripts = [];

  var inelems = indoc.getElementsByTagName(&#039;script&#039;);
  for(var i = 0; i &lt; inelems.length; i++) {
    var script_children = inelems[i].childNodes;
    var script_text=&#039;&#039;;
    for(var j = 0; j &lt; script_children.length; j++) {
      script_text += script_children[j].data;
    }
    outscripts.push(script_text);
    inelems[i].parentNode.removeChild(inelems[i]);
  }
  return outscripts;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then, after loading the new XML document over the network, but before inserting it into the main document tree, do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var ajscripts = ajax_getscripts(xhttp.responseXML);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And finally, after you&#039;ve loaded the new XML document/fragment into the main document tree, execute the code like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(var i = 0;i&lt;ajscripts.length;i++) { setTimeout(ajscripts[i],0); }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Everything works &quot;as it should&quot; AFAIK, even immediately executed code (outside of subroutine definitions) that accesses elements in the newly loaded fragment and/or the original document.&lt;/p&gt;

&lt;p&gt;The only thing that doesn&#039;t work &quot;as it should&quot; is that onLoad attributes aren&#039;t executed for elements in the newly received document.  It would seem relatively trivial to add this support, but there are some corner cases (like the use of &quot;this&quot; in the context of an onLoad code string) that make it too tricky to be worth the effort IMHO.  Anything you could do from onLoad can be done from within an appropriately placed  node anyways.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been solving the same problem recently, and it can be done even simpler.  setTimeout() itself evals a string argument, therefore there&#8217;s no need for the escaping of single-quotes via replace() or the extra layer of eval, just do setTimeout(codestring,0); where codestring is a variable containing the js code from the  tags.  Here was the complete thing I used in my ajax library:</p>

<pre><code>// Strip  elements out of an XML document retreived via xmlHttpRequest,
// returning them as an array of strings

function ajax_getscripts(indoc) {
  var outscripts = [];

  var inelems = indoc.getElementsByTagName('script');
  for(var i = 0; i I've been solving the same problem recently, and it can be done even simpler.  setTimeout() itself evals a string argument, therefore there's no need for the escaping of single-quotes via replace() or the extra layer of eval, just do setTimeout(codestring,0); where codestring is a variable containing the js code from the  tags.  Here was the complete thing I used in my ajax library:

// Strip  elements out of an XML document retreived via xmlHttpRequest,
// returning them as an array of strings

function ajax_getscripts(indoc) {
  var outscripts = [];

  var inelems = indoc.getElementsByTagName('script');
  for(var i = 0; i &lt; inelems.length; i++) {
    var script_children = inelems[i].childNodes;
    var script_text='';
    for(var j = 0; j &lt; script_children.length; j++) {
      script_text += script_children[j].data;
    }
    outscripts.push(script_text);
    inelems[i].parentNode.removeChild(inelems[i]);
  }
  return outscripts;
}
</code></pre>

<p>And then, after loading the new XML document over the network, but before inserting it into the main document tree, do:</p>

<pre><code>var ajscripts = ajax_getscripts(xhttp.responseXML);
</code></pre>

<p>And finally, after you&#8217;ve loaded the new XML document/fragment into the main document tree, execute the code like so:</p>

<pre><code>for(var i = 0;i&lt;ajscripts.length;i++) { setTimeout(ajscripts[i],0); }
</code></pre>

<p>Everything works &#8220;as it should&#8221; AFAIK, even immediately executed code (outside of subroutine definitions) that accesses elements in the newly loaded fragment and/or the original document.</p>

<p>The only thing that doesn&#8217;t work &#8220;as it should&#8221; is that onLoad attributes aren&#8217;t executed for elements in the newly received document.  It would seem relatively trivial to add this support, but there are some corner cases (like the use of &#8220;this&#8221; in the context of an onLoad code string) that make it too tricky to be worth the effort IMHO.  Anything you could do from onLoad can be done from within an appropriately placed  node anyways.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Black</title>
		<link>http://nerd.metrocat.org/2005/09/the-magic-eval/comment-page-1#comment-22</link>
		<dc:creator>Brandon Black</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://nerd.metrocat.org/2005/09/the-magic-eval#comment-22</guid>
		<description>&lt;p&gt;Something with this Markdown stuff doubled-up part of my post when I edited it, it should be obvious what :)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Something with this Markdown stuff doubled-up part of my post when I edited it, it should be obvious what :)</p>]]></content:encoded>
	</item>
</channel>
</rss>
