Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

try/catch in IE4/NS4

Status
Not open for further replies.

dd4005

Programmer
Sep 1, 2005
12
DE
Although IE4/NS4 don't support try/catch functionality, they do have those names as reserved keywords. Clearly they were planning to support it.

If you have a block of JS with try/catch anywhere within it, even if the code isn't executed by IE4/NS4 due to logic/sniffing, it still causes a problem with the parser. The error is something like "try is a reserved keyword".

I wondered if anyone had made some kind of workaround that allows them to use try/catch in these browsers without having to resort to using eval. I have the occasional use in that way, like this:

Code:
if(!V4Browser)
    eval("try{something dangerous;}catch(e){}");

but that's not so easy when the something dangerous is a lot of code.

Anyone else worked around this in another way?

~dd
 
When I said I want to "USE" try/catch in IE4/NS4 I meant I simply want to have it present in the javascript files. I wouldn't be trying to USE it. Sniffing would cause those browsers to avoid the try/catch code.
 
You could wrap the try statement in a try/catch block to stop the errors appearing [wink]

On a more serious note... it's an interesting problem. You can't detect the presence of try being supported with things like:

Code:
if (try)

One thing you might try is specifying a version of JavaScript. I've not tried this myself, but it might just work. Apparently, try/catch was implemented in JS1.4, so using this:

Code:
<script type="text/javascript" language="JavaScript1.4">

for the piece of code that houses the try/catch statement would probably stop errors happening on older browsers that only supported a lesser version of JS.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Dan, I'd already looked into that. Unfortunately IE (even version 6) still is only compatible with Javascript 1.2. Unless someone comes up with something better it looks like keeping it hidden inside eval statements that are not executed by IE4/NS4 is the safest way.

~dd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top