Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28717

Re: alternatives to eval() for this case

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: alternatives to eval() for this case
Date 2015-09-26 00:35 -0300
Organization Aioe.org NNTP Server
Message-ID <mu53pe$ktl$1@speranza.aioe.org> (permalink)
References <1a8362bb-6ff2-41a9-93b2-5469b67ffd02@googlegroups.com>

Show all headers | View raw


On 25/09/15 14:26, Jay Braun wrote:
> Hello.  I have read much of the on-line material about the drawbacks
> of eval().

eval() isn’t evil, just misunderstood

<http://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/>


> One of the few cases where eval() seems to be regarded as
> acceptable is an AJAX case like this, where a script on the server
> side is being requested:

There are other ways to insert a script into a document than using AJAX

>
> function evalRequest(url) {
>   var xmlhttp = new XMLHttpRequest();
>   xmlhttp.open("GET", filename); //perhaps set request header
>   xmlhttp.send();
>   xmlhttp.onreadystatechange = function() {
>     if (xmlhttp.readyState==4 && xmlhttp.status==200) {

Here you might check the Content-Type to be sure of the file type, e.g.

var rsp, contentType = xhr.getResponseHeader("Content-Type");
if (/json|javascript/i.test(contentType)) {
   rsp = xhr.responseText;
   // Or use JSON.parse if it is a JSON file
}
// use a callback function passed to evalRequest() to do the job
// (insert the script):
insertScript(rsp, 'newJS');

>     }
>   }
>   xmlhttp.open("GET", url, true);
>   xmlhttp.send(null);
> }
>

Example of such a callback function:

function insertScript(src, id) {
   var script = document.createElement('script');
   script.src = src;
   script.async = false; // HTML5
   document.head.appendChild(script);
}

> which is exactly the type of case where I am using it.  It works
> great when the requested script is "reasonably" sized, e.g., tens of
> KB.  But occasionally, the server-side application processes so much
> data that the file might be tens of MB in length.  eval() is not only
> slow (as expected), but does not complete the parsing of the file.

I can't imagine a script file so big...

The problem, I think, is if you need the scripts to execute in an 
expected order. If so, you may try to mark the downloaded script as not 
async [1], using:

script.async = false;

[1] <http://caniuse.com/#feat=script-async>


-- 
Joao Rodrigues

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

alternatives to eval() for this case Jay Braun <lyngwyst@gmail.com> - 2015-09-25 10:26 -0700
  Re: alternatives to eval() for this case Danny <dann90038@gmail.com> - 2015-09-25 15:39 -0700
    Re: alternatives to eval() for this case Jay Braun <lyngwyst@gmail.com> - 2015-09-25 15:50 -0700
  Re: alternatives to eval() for this case Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-09-26 00:35 -0300
    Re: alternatives to eval() for this case Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-26 09:33 +0200
  Re: alternatives to eval() for this case Jay Braun <lyngwyst@gmail.com> - 2015-09-26 20:14 -0700
    Re: alternatives to eval() for this case Martin Honnen <mahotrash@yahoo.de> - 2015-09-27 10:30 +0200
      Re: alternatives to eval() for this case Jay Braun <lyngwyst@gmail.com> - 2015-09-27 07:45 -0700

csiph-web