[Dojo-interest] Invoking Javascript [was: FetchingHTMLandinvoking javascript with io.bind]

Tom Whitcomb tom.whitcomb at digitalfocus.com
Fri May 27 19:55:55 PDT 2005


Cool.  I use fetched javascript in the same way only I still found it
really valuable to use an iframe for a separate namespace.

You may have already tried this approach, but...

The fetched javascript can be written to an iframe that can access the
existing reusable functions via a parent.function() call.  In turn, the
parent can reference its child iframe functions and properties as well.

Below is kind of a hack, but it shows what I'm trying to explain.

The NSAlloc function takes "<script>....</script>" in a string, writes it
to  an iframe and then returns the iframe's window.  The javascript passed
into NSAlloc references resuable functions in the parent and conversely
the parent can reference functions and properties in the child.

function NSAlloc(code){
  var i = document.createElement('iframe');
  i.style.visibility = 'hidden';

  document.body.appendChild(i);
  var d = i.contentWindow?i.contentWindow.document:i.contentDocument;
  d.open();  d.write(s);  d.close();

  var win = i.contentWindow?i.contentWindow:i.contentDocument.defaultView;
  win.allocNS = i;  // save the iframe reference
  return win;
}

So, I can do stuff like:

  var x = 1;
  var child = NSAlloc("<script>var y=2; alert(parent.x);</script>");
  alert(child.y);
  NSFree(child);

I store the iframe's address in the returned window object because I later
free it with NSFree(win);

The part I really like is I don't have to deal with name clashes.

Tom

> I actually want the new code to execute in the same context.  I have
> some common user interface related functions that the loaded
> javascript needs access to.  For example, when I pull in tabular
> results, I call a method to set my next and previous links in the
> result widget.
>
> Josh
>
> On 5/27/05, Tom Whitcomb <tom.whitcomb at digitalfocus.com> wrote:
>> Definitely nice and very useful.
>>
>> Wouldn't you be concerned that the fetched/executed javascript could
>> collide with existing names in the current context?  I've always relied
>> upon an iframe to isolate any fetched javascript so I could be sure no
>> collisions occured.  Why do you prefer this approach over an iframe
>> approach?
>>
>> Tom
>>
>>
>> > Cool stuff. Couple of comments:
>> >
>> > Why not stuff the content into the container, then node-walk to find
>> the
>> > scripts instead of extracting them with RegEx? That would avoid the
>> > debug-the-regex step.
>> >
>> > Wrt IFRAME transport, maybe one could use a custom tag for embedded
>> > scripts
>> > (dscript or something) to avoid auto-executing those bits. Probably
>> easier
>> > just to detect the transport model and avoid calling the extra
>> execution
>> > step.
>> >
>> > Scott
>> >
>> > -----Original Message-----
>> > From: dojo-interest-bounces at dojotoolkit.org
>> > [mailto:dojo-interest-bounces at dojotoolkit.org] On Behalf Of Josh Reed
>> > Sent: Friday, May 27, 2005 10:29 AM
>> > To: Dojo-interest at dojotoolkit.org
>> > Subject: [Dojo-interest] Invoking Javascript [was: Fetching HTML
>> > andinvoking
>> > javascript with io.bind]
>> >
>> > Hello all,
>> >
>> > Sorry for jumping out the previous thread, but I just joined the
>> > mailing list.  I was browsing the archives and saw the thread.
>> >
>> > I'm using io.bind in an interface that I'm building and I addressed
>> > this issue of pulling back HTML and invoking javascript embedded in
>> > it.  I went ahead and coded up a javascript method to do it.  Check
>> > out: http://cws3.geol.iastate.edu:8080/xqe/scripts/dhtml.js
>> >
>> > The function in question is called executeScripts(). Basically it
>> > looks through the returned content and requests external scripts (e.g.
>> > they have a src="" attribute).  If the script is inline, it evals().
>> > Currently, I think it only works for inlined scripts on a single line
>> > e.g.
>> >
>> > <script>alert('hi')</script>
>> >
>> > but not
>> >
>> > <script>
>> >   alert('hi!');
>> > </script>
>> >
>> > I don't know how well this approach will work with other
>> > transports...it seems like an iframe wouldn't need to worry about it
>> > because the scripts should get executed as part of the loading.  Dunno
>> > though.
>> >
>> > I think it is just a matter of some debugging on my part.  Anyhow,
>> > seems to work well across browsers.  I use the technique fairly
>> > extensively in the interface at:
>> > http://cws3.geol.iastate.edu:8080/xqe/
>> >
>> > Thanks,
>> > Josh Reed
>> >
>> >
>> > --
>> > No virus found in this outgoing message.
>> > Checked by AVG Anti-Virus.
>> > Version: 7.0.322 / Virus Database: 267.1.0 - Release Date: 5/27/2005
>> >
>> >
>> > _______________________________________________
>> > Dojo-interest mailing list
>> > Dojo-interest at dojotoolkit.org
>> > http://dojotoolkit.org/mailman/listinfo/dojo-interest
>> >
>>
>>
>> Digital Focus  |  Tom Whitcomb  |  CTO  | 703 561 5884
>> _______________________________________________
>> Dojo-interest mailing list
>> Dojo-interest at dojotoolkit.org
>> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>>
> _______________________________________________
> Dojo-interest mailing list
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>


Digital Focus  |  Tom Whitcomb  |  CTO  | 703 561 5884


More information about the Dojo-interest mailing list