Is there a good way to tell jQuery not eval scripts for ajax requests that are HTML

I am using jQuery to create an ajax request and everything works as expected. But he evaluates the scenarios in the wrong context.

I have an iframe (same domain, no worries here), and I'm trying to get the scripts for eval in the iframe context, instead of where the ajax request was made - for example. another frame.

I thought I could say ajax so as not to analyze the scripts, and I would do the work myself to evaluate them in the right context. Any ideas or ways to turn off automatic pricing?

Edit

So, I was somewhat wrong with the original question. Scripts are not evaluated when loading, but rather when content is placed in a document. You can see this by checking an example:

$('#some_element').html('<span>content</span><script>alert(window)</script>'); 

Now, when you do this from another frame, the evaluation takes place in the area where you are calling, and not in the element into which you are inserting content.

I ended up setting the content without using jQuery, and then discovered / rejected the script tags:

 element.get(0).innerHTML = data; element.find('script').each(function() { otherWindow.eval(this.innerText); }); 

Final update

I ended up tracking it to the source and canceled it from there. The next one is coffeescript, but you can get this idea. I decided to override it, because for my use this should never happen in the top window, but is expected in the iframe content.

  $.globalEval = (data) -> (iframeWindow.execScript || (data) -> iframeWindow["eval"].call(iframeWindow, data))(data) if (data && /\S/.test(data)) 
+4
source share
1 answer

This question shows how to perform custom script evaluations:

jQuery: Rate a script in ajax answer

In the following code snippet ... ** this code is the answer to another question **, just got it as a snippet:

  $("body").append($(xml).find("html-to-insert").eq(0)); eval($(xml).find("script").text()); 

eval itself is tied to a window, which you can define as a context:

windowObject.eval - when you call only eval ('...'), it assumes that you call like this: window.eval('...')

Now you need to get a window corresponding to the frame in which you want to execute eval and do something like this:

myIFrameWindow.eval('...')

When you do this, it runs in the context of this window. It's just a matter of finding the iframe-related window you want now.

To find a window with a given frame, check out this post:

Getting the IFRAME window (and then the document) Links With contentWindow

+1
source

All Articles