var loadScript = function(url) {
var e = document.createElement("script");
e.src = url;
e.type = "text/javascript";
h.appendChild(e); // h is a reference to the head element
};
If the file has no errors, everything loads/runs/is available as
expected. If the file has, say, a syntax error then just after the
appendChild the entire page stops working.
Given that I can't guarantee that the file will always be error free how
can I "catch" any error. That is, an error in the file shouldn't kill
the page.
I tried wrapping everything in try/catch but that didn't do anything.
Its almost like I need a way to check the syntax of the external file
before I append it.
Andrew Poulos
Do you have a demo? I can't confirm this. In my simple tests, the
page continues to work, and the code immediately after a call to
loadScript still runs properly.
http://scott.sauyet.com/Javascript/Demo/2010-02-12a/
> Given that I can't guarantee that the file will always be error free how
> can I "catch" any error. That is, an error in the file shouldn't kill
> the page.
If you can't guarantee that the file is error-free, can you guarantee
that it doesn't contain malicious code?
> I tried wrapping everything in try/catch but that didn't do anything.
>
> Its almost like I need a way to check the syntax of the external file
> before I append it.
Well sure, it shouldn't take more than an hour or two to whip up a
full-fledged ECMAScript parser inside ES, right? :-)
-- scott
Let me see what You're doing and I'll then put a demo together. (IN case
I'm doing something silly).
>> Given that I can't guarantee that the file will always be error free how
>> can I "catch" any error. That is, an error in the file shouldn't kill
>> the page.
>
> If you can't guarantee that the file is error-free, can you guarantee
> that it doesn't contain malicious code?
Yes. I can guarantee that it doesn't contain intentionally malicious code.
>> I tried wrapping everything in try/catch but that didn't do anything.
>>
>> Its almost like I need a way to check the syntax of the external file
>> before I append it.
>
> Well sure, it shouldn't take more than an hour or two to whip up a
> full-fledged ECMAScript parser inside ES, right? :-)
:-)
Andrew Poulos
Ok, it is working for me as well. The external js calls some inline js
that sets some other things. As the other things were not getting set I
wrongly assumed that the "bad" external js was causing the issue.
>>> Given that I can't guarantee that the file will always be error free how
>>> can I "catch" any error. That is, an error in the file shouldn't kill
>>> the page.
What I'd now like to do is to get some notification that the extrnal js
has an error. Is there some way, except for a time out, to "know" if
there was an error?
Andrew Poulos
The only way I've ever managed that is with timeouts, usually a
repeated check for the expected changes from the external file and a
function that runs after a timeout if those expected changes haven't
been applied. It's fine for very specific cases, but I've never
developed a more general solution to the problem. If you do fine one,
please let me know! :-)
Good luck,
-- Scott