How to parse text as JavaScript?

Posted by Danjah on Stack Overflow See other posts from Stack Overflow or by Danjah
Published on 2010-06-18T06:40:55Z Indexed on 2010/06/18 6:43 UTC
Read the original article Hit count: 272

This question of mine (currently unanswered), drove me toward finding a better solution to what I'm attempting.

My requirements:

  • chunks of code which can be arbitrarily added into a document, without an identifier:

    [div class="thing"] [elements... /] [/div]

  • the objects are scanned for and found by an external script:

    var things = yd.getElementsBy(function(el){ return yd.hasClass('thing'); },null,document );

  • the objects must be individually configurable, what I have currently is identifier-based:

[div class="thing" id="thing0"] [elements... /] [script type="text/javascript"] new Thing().init({ id:'thing0'; }); [/script] [/div]

  • So I need to ditch the identifier (id="thing0") so there are no duplicates when more than one chunk of the same code is added to a page

  • I still need to be able to config these objects individually, without an identifier


SO! All of that said, I wondered about creating a dynamic global variable within the script block of each added chunk of code, within its script tag. As each 'thing' is found, I figure it would be legit to grab the innerHTML of the script tag and somehow convert that text into a useable JS object.

Discuss.

Ok, don't discuss if you like, but if you get the drift then feel free to correct my wayward thinking or provide a better solution - please!

d

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about configuration