title: Is there a nice way to tell jQuery to not eval scripts on ajax requests that are HTML
body: <p>I'm using jQuery to make an ajax request, and everything is working as expected.  But it's evaluating the scripts in the wrong context.</p>
  
  <p>I have an iframe (same domain, no worries here), and I'm trying to get the scripts to eval in the context of the iframe, instead of where the ajax request was made -- eg. a different frame.</p>
  
  <p>I figured I could tell ajax not to eval the scripts, and I would do the work myself to eval them in the right context.  Any ideas, or ways to disabled the automatic eval?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>So, I was somewhat wrong about the initial question.. The scripts are not evaluated when loaded, but rather when the content is being placed in the document.  You can see this by testing the example:</p>
  
  <pre><code>$('#some_element').html('&lt;span&gt;content&lt;/span&gt;&lt;script&gt;alert(window)&lt;/script&gt;');
  </code></pre>
  
  <p>Now, when doing this from a different frame, the evaluation happens in the scope of where you're calling, not the element you're inserting content into.</p>
  
  <p>I ended up setting the content without using jQuery, and then finding/evaling any script tags:</p>
  
  <pre><code>element.get(0).innerHTML = data;
  element.find('script').each(function() {
    otherWindow.eval(this.innerText);
  });
  </code></pre>
  
  <p><strong>Final Update</strong></p>
  
  <p>I ended up tracking it down to the source, and overriding it from there.. the following is coffeescript, but you can get the idea.  I chose to override it because for my usage, this should never happen in the top window, but is expected in the iframe content.</p>
  
  <pre><code>    $.globalEval = (data) -&gt; (iframeWindow.execScript || (data) -&gt; iframeWindow["eval"].call(iframeWindow, data))(data) if (data &amp;&amp; /\S/.test(data))
  </code></pre>