Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2009



Tip: Looking for answers? Try searching our database.

Running code from a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ofiras - 31 Jul 2009 16:22 GMT
Hi,
I have a JavaScript code that fetches code lines from another page
into a string, but I don't know how to run this code in the page I
need.
I've tried this but it didn't work:

var code; //my javascript code
document.getElementById('tojs1').innerHTML = 'function fill_mish1()
{'+ code +'}';
fill_mish1();

While in my page there is a line:

<script id='tojs1' type=text/javascript> </script>

Please help,
Ofir.
Martin Honnen - 31 Jul 2009 16:28 GMT
> I have a JavaScript code that fetches code lines from another page
> into a string, but I don't know how to run this code in the page I
[quoted text clipped - 5 lines]
> {'+ code +'}';
> fill_mish1();

That sounds like a justified use of eval:
  eval(code);

Signature

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/

ofiras - 31 Jul 2009 20:40 GMT
> > I have a JavaScript code that fetches code lines from another page
> > into a string, but I don't know how to run this code in the page I
[quoted text clipped - 13 lines]
>         Martin Honnen
>        http://msmvps.com/blogs/martin_honnen/

thanks!
Gordon - 31 Jul 2009 17:26 GMT
> Hi,
> I have a JavaScript code that fetches code lines from another page
[quoted text clipped - 13 lines]
> Please help,
> Ofir.

You can use eval() to run a string as a piece of JavaScript code.  But
please, for the love of fluffy kittens, be extremely careful with
this!  The potential for abuse should be obvious, and in addition, eval
()ed code runs more slowly, especially in the newfangled JIT
JavaScript engines that are in vogue at the moment because it can't be
sensibly compiled for future use.  You'll also have a hell of a time
debugging a wayward piece of eval()ed code.
Jorge - 01 Aug 2009 00:54 GMT
> Hi,
> I have a JavaScript code that fetches code lines from another page
[quoted text clipped - 13 lines]
> Please help,
> Ofir.

Use eval() as Martin pointed out, or (new Function(code))(); The
former will -usually- run 'code' in the current context, the latter
will execute it in the context of an anonymous, top-level function :
i.e. it is equivalent to :

<script>
(function () { code })();
</script>

--
Jorge.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.