Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Catch errors in dynamically loaded script m

4 views
Skip to first unread message

Andrew Poulos

unread,
Feb 12, 2010, 2:40:04 AM2/12/10
to
I'm using the following to dynamically load an externally generated
script file:

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

Scott Sauyet

unread,
Feb 12, 2010, 7:48:45 AM2/12/10
to
On Feb 12, 2:40 am, Andrew Poulos <ap_p...@hotmail.com> wrote:
> I'm using the following to dynamically load an externally generated
> script file:
>
> [ ... straightforward script loader ... ]

>
> 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.

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

Andrew Poulos

unread,
Feb 12, 2010, 2:46:51 PM2/12/10
to
On 12/02/2010 11:48 PM, Scott Sauyet wrote:
> On Feb 12, 2:40 am, Andrew Poulos<ap_p...@hotmail.com> wrote:
>> I'm using the following to dynamically load an externally generated
>> script file:
>>
>> [ ... straightforward script loader ... ]
>>
>> 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.
>
> 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/

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

Andrew Poulos

unread,
Feb 13, 2010, 1:10:07 AM2/13/10
to
On 13/02/2010 6:46 AM, Andrew Poulos wrote:
> On 12/02/2010 11:48 PM, Scott Sauyet wrote:
>> On Feb 12, 2:40 am, Andrew Poulos<ap_p...@hotmail.com> wrote:
>>> I'm using the following to dynamically load an externally generated
>>> script file:
>>>
>>> [ ... straightforward script loader ... ]
>>>
>>> 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.
>>
>> 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/
>
> Let me see what You're doing and I'll then put a demo together. (IN case
> I'm doing something silly).

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

Scott Sauyet

unread,
Feb 13, 2010, 12:23:08 PM2/13/10
to
On Feb 13, 1:10 am, Andrew Poulos <ap_p...@hotmail.com> wrote:
> 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?

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

0 new messages