Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Dr J R Stockton Newsgroups: comp.lang.javascript Subject: Re: Parsing String of Named Function & Converting To Source Date: Wed, 19 Oct 2011 20:09:52 +0100 Organization: Home Lines: 64 Message-ID: References: <8voi2r92.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain;charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="UwYDIHu13GPq28RciNoAJA"; logging-data="20050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+K1tA4Q+rj8YSZ5WACbRM07vzJ6JaBePE=" User-Agent: Turnpike/6.05-S () Cancel-Lock: sha1:Pd8LxEe0gGHGsso6rouCZp7J2s4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:7572 In comp.lang.javascript message <8voi2r92.fsf@gmail.com>, Tue, 18 Oct 2011 18:34:17, Lasse Reichstein Nielsen posted: >Seni Seven writes: > >> Suppose I have HTML markup with a SCRIPT element as shown below: >> >> >> >> 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 - 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 - 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 .