mpen
mpen

Reputation: 282855

How to pass jQuery to eval'd code?

First, check out my fiddle fiddle

jsFiddle is nice, but sometimes it runs a bit slow, so I made this simple pure JS fiddle (can be ran locally)

Working great so far, but I want to be able to use jQuery in my fiddle. Type alert(typeof $); in the bottom left box and press "Run" to see what I mean (says undefined).

So how do I pass jQuery off to the iframe and give it the correct context? I suspect I could set doc.contentWindow.$=$ or something like that, but I still need to give the correct context so it knows where to find the elements. How would I do that?


iframe.contentWindow.$ = $.proxy($,iframe.contentWindow);

That didn't work either. Gives me access to jQuery, but the context is still wrong.


iframe.contentWindow.$ = function(s){return $(s,doc);};

That sort of works... but it's a bit of a hack.

Upvotes: 2

Views: 225

Answers (2)

gilly3
gilly3

Reputation: 91497

I tweaked your fiddle fiddle a little: http://jsfiddle.net/gilly3/XcSYz/3/

First, you can't write a doctype to the body's innerHTML and expect it to do anything for you. Same with a charset meta tag. I moved all of that to a document.write call.

You were evaluating script tags oninput of the editors. First this throws errors like crazy if you are editing a script tag in the html frame. Second, if you put an alert("hello") in a script tag in the HTML pane, then with every keypress, you get an alert. Third, you were evaluating the scripts in the context of the parent window. I changed html script tags to be evaluated when run is clicked and with the correct context.

Since you don't want to re-write the document every time (you can't, eg doctype), I modified it slightly to do some dom manipulation instead. It removes and replaces the style tag and writes only the HTML pane contents to the body's innerHTML.

This is a fun little project you are trying. jsFiddle is buggy and needs some updates. I don't know how viable your pure client-side fiddle is - jsFiddle is quite robust (despite its bugs), but it is a nice exercise to give your approach a try.

Edit: One more change included in this version: I modified your textareas to use width: 50%; height: 50%; instead of setting right and bottom. Those weren't working properly in IE or FF.

Upvotes: 1

John Flatness
John Flatness

Reputation: 33759

Why not do as jsfiddle itself does? It simply includes a script tag for jQuery (or whatever framework you asked for) in the head of the iframe.

If you look in head of the... uh "outer" iframe (the one jsfiddle created), you'll see they just threw this in there:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>

Same thing applies to the "on ready" and "on load" options, jsfiddle just wraps the appropriate call around your javascript before injecting it into the iframe.

Modifying your code a little, you can create a script element:

var doc = iframe.contentWindow.document;
var jqueryTag = doc.createElement('script');
jqueryTag.type = 'text/javascript';
jqueryTag.src = 'http://code.jquery.com/jquery-1.6.4.js';

and then append that element to the head.

Upvotes: 1

Related Questions