2

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Why not embed it inside using a ` – Pekka Oct 06 '11 at 22:17
  • You made a fiddle in a fiddle?! That's meta. – Gazler Oct 06 '11 at 22:18
  • This may be too Meta for me, I'm not sure I get it correctly. :) – Pekka Oct 06 '11 at 22:24
  • @Pekka: But is it really too meta? http://min.us/lbw1Ce3eY2l4hb (i couldn't get it to go down another level :( – mpen Oct 06 '11 at 23:38
  • @Mark no, it was just too Meta for *me*, I was at the end of a very exhaustiv work day :) – Pekka Oct 07 '11 at 08:43
  • @Pekka: there we go... http://i.minus.com/ib0Yf8w8UT8VML.png got it to go a couple levels deeper thanks to John and gilly! the button got in my way of going any further :( – mpen Oct 07 '11 at 15:58

2 Answers2

1

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.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • I keep getting a syntax error when I try doing that (http://jsfiddle.net/XcSYz/1/) not sure why... my IDE (gedit) looks like it terminates the string as soon as I type the `>` in ``, as though it's trying to close the actual ` – mpen Oct 06 '11 at 22:30
  • 1
    Browsers will see the `` in your string and end the script block in the middle of things, you need to re-jigger a little: `\x3Cscript type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js">\x3C/script>`. [Source](http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write). – John Flatness Oct 06 '11 at 22:37
  • Thought so! Thanks. That got rid of the JS error, but jQuery still isn't accessible. – mpen Oct 06 '11 at 22:41
  • 1
    I had better luck when using `createElement` (also happily avoids the script closing tag business): [see fiddle](http://jsfiddle.net/bNYf4/3/) (that also addresses some other weirdness you had where you were appending everything to the `body`). – John Flatness Oct 06 '11 at 23:04
  • Nice! That fixed it up. I think this tool should be pretty handy now...lovin' the live preview :D – mpen Oct 06 '11 at 23:25
1

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.

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • 1st paragraph: oops. didn't think that one through. 2nd: yes, i realized the same thing which is why i added the "run" button, but then i forgot to remove the old code. i might have a checkbox though to have it continuously run when you're editing the html. thanks for the tips/help! i might try seeing how they did the syntax highlighting and indentation later. – mpen Oct 07 '11 at 00:03
  • why are you removing the ` – mpen Oct 07 '11 at 00:07
  • @Mark - I didn't think you could replace the "html" of a style tag at runtime, but tried and sure enough, it works: http://jsfiddle.net/gilly3/XcSYz/7/. I was being cautious, trying to prevent old styles from sticking around, but it looks like either way works. – gilly3 Oct 07 '11 at 00:21
  • This is definitely an improvement on the "fiddle fiddle", but I'm not sure it answers the question (other than in the code of the linked fiddle, that is). – John Flatness Oct 07 '11 at 00:50
  • @JohnFlatness: True that. I liked this solution better so I gave him the check, but I guess you're still the one that solved the problem :) You can have it back! – mpen Oct 07 '11 at 15:21
  • @gilly3: Ah..good to know. I was only testing in Chrome so far. The borders seem to be gone with your new version though. Oh wow...it really does look awful in Firefox (the old one) – mpen Oct 07 '11 at 15:22
  • I really wish you could do 50% minus 1 px. I think the other div is covering up the border. Oh maybe if I set a z-index... (talking to self) **edit:** yup...that actually worked. http://jsfiddle.net/mnbayazit/XcSYz/8/ – mpen Oct 07 '11 at 15:44