Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7572
| From | Dr J R Stockton <reply1142@merlyn.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Parsing String of Named Function & Converting To Source |
| Date | 2011-10-19 20:09 +0100 |
| Organization | Home |
| Message-ID | <fyJIlYLACynOFwa3@invalid.uk.co.demon.merlyn.invalid> (permalink) |
| References | <Xns9F82AB08718BBSSca@88.198.244.100> <8voi2r92.fsf@gmail.com> |
In comp.lang.javascript message <8voi2r92.fsf@gmail.com>, Tue, 18 Oct
2011 18:34:17, Lasse Reichstein Nielsen <lrn.unread@gmail.com> posted:
>Seni Seven <OneWhoLovesYou@humanitarian.ca.invalid> writes:
>
>> Suppose I have HTML markup with a SCRIPT element as shown below:
>>
>> <script type="text/javascript">
>> function multiply(a, b) {
>> return a * b;
>> }
>> var i = multiply(2, 5);
>> document.write("The value of <i>i</i> is " + i);
>> </script>
>>
>> All of this markup and contained script code is retrieved as a string using
>> Ajax.
>> I have an HTML parser with support for parsing the string containing the
>> script code.
>> In parsing the contained code (using JavaScript, of course), how would you
>> convert the contained script code from string into source?
>> ...
>> the value of identifier 'i' is immediately set to 10 in the debugger, but
>> the debugger exits with error 'multiply is not defined', which indicates
>> multiple levels of execution contexts, I suppose. In fact, if I just do an
>> eval() on the "multiply" assignment of the Function constructor, the
>> identifier 'multiply' is still not defined.
>>
>> QUESTION: What's the solution to the goal I want to achieve?
I've done that, or something rather similar, in
<http://www.merlyn.demon.co.uk/js-randm.htm> - the code for inserting
Johannes Baagoe's (or other) functions. IIRC, it was necessary to be
slightly subtle about the argument to 'eval', perhaps such as something
like prepending " XXX = " to the function - see the source code of the
page.
>Try:
> var script = document.createElement("script");
> script.textContent = sourceString;
> document.body.appendChild(script);
>This executes the code as top-level, non-eval code (which there is no way
>to do in pure Javascript).
>No guarantees wrt. old browsers, but it seems to work in the current versions.
>
>Alternatively, just use a non-direct call to eval.
> var topLevelEval = eval;
> topLevelEval(sourceString);
Would any of your contribution give a worthwhile improvement in the
cited js-randm.htm, given that the latter works?
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike 6.05 WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar
Parsing String of Named Function & Converting To Source Seni Seven <OneWhoLovesYou@humanitarian.ca.invalid> - 2011-10-18 13:48 +0000
Re: Parsing String of Named Function & Converting To Source Andreas Bergmaier <andber93@web.de> - 2011-10-18 16:03 +0200
Re: Parsing String of Named Function & Converting To Source Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 19:29 +0200
Re: Parsing String of Named Function & Converting To Source Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-18 18:34 +0200
Re: Parsing String of Named Function & Converting To Source Seni Seven <OneWhoLovesYou@humanitarian.ca.invalid> - 2011-10-19 08:01 +0000
Re: Parsing String of Named Function & Converting To Source Dr J R Stockton <reply1142@merlyn.demon.co.uk> - 2011-10-19 20:09 +0100
csiph-web