1

Here I have asked a question pertaining to exceptions raised when dynamically loading scripts via XMLHttpRequest (in other words when executed via eval)

In a related question, I wanted to know whether loading scripts dynamically as such is considered bad practice to begin with. In my particular case I have an HTML Canvas element, and rather than load all possible shapes, I want to fetch them dynamically, without reloading the page, and execute them on return. The problem I am having there is that if the code associated with that shape is incorrect, the error message displayed is not very useful (indicates location of eval statement, not incorrect statement). Is there another way to dynamically fetch code from the server and execute it, while better informing the location of the exception when it occurs.

Community
  • 1
  • 1
puk
  • 16,318
  • 29
  • 119
  • 199

2 Answers2

2

If you want to load a script use a <script> element. If you want to dynamically load a script, create the <script> element dynamically.

var script = document.createElement('SCRIPT');
script.src = "<url to load>";
document.getElementsByTagName("HEAD")[0].appendChild(script);

It's not guaranteed to be synchronous the way eval with synchronous XHR is, but ideally you'd structure your code to take advantage of asynchony.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • if this caused an exception, would the line number be indicated? I need to carry out some more complicated tasks, so this might not suffice. For example, my scripts need to indicate what other scripts to load. – puk Oct 24 '11 at 00:49
  • Just out of curiosity, is there really a benefit to include as opposed to eval? I'd assume include pretty much does the same thing as fetching the code and evaluating it. – puk Oct 24 '11 at 00:50
  • @puk, Yes, errors in code loaded via ` – Mike Samuel Oct 24 '11 at 13:23
1

Adding to Mike's answer, if you want good debugger support including a script tag is probably the way to go, since that is what debuggers are used to working on. The main differences from eval that you need to be aware of are:

  • Eval runs in the scope of where its called while included script tags run in the global scope.
  • Eval is synchronous while included tags run asynchronously. (You will need to use something like JSONP and dojo.io.script if you need to run code after the script tag finishes).

If the scripts are fixed you can also consider debugging them by including their script tags and deploying them as you see fit.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • looks like there is no way I can get what I want via `eval`. Could you elaborate on your last comment. It sounded like you told me to include it, and then if it works, eval it. – puk Oct 24 '11 at 01:37
  • Well, if you know beforehand that the script won't throw an error it doesn't matter too much what method you use, right? :) – hugomg Oct 24 '11 at 01:58
  • life is never that simple when it comes to coding :-) My program is so complex that I can no longer test the scripts independently (there is a great deal of linking and recursion). – puk Oct 24 '11 at 02:02