Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Catch not catching syntax error in Chrome

I'm trying to troubleshoot a problem with someone else's JavaScript file, and they declared a function like the following.

function window.confirm(str) {
..... code here .....
}

This works fine with IE, but in Google Chrome, it throws an uncaught syntax error on the period in window.confirm. I tried to put a try catch around it like below, but that didn't work, same syntax error. It then won't let me use any functions defined in that JavaScript file.

try {
    var window.confirm = function(str) {
        ..... code here .....
    };
}
catch(e) {}

I also tried to change the declaration to a variable, like below, but that didn't work either. Same error.

var window.confirm = function(str) {
    ..... code here .....
};

Is there a way to catch this in Chrome?

like image 236
Andrew Avatar asked Jun 13 '26 07:06

Andrew


2 Answers

function window.confirm(str) and var window.confirm ... are invalid. instead, try:

window.confirm = function(str){
..... code here .....
}
like image 74
Joshua Dwire Avatar answered Jun 14 '26 21:06

Joshua Dwire


Two points :

  • try/catch are used to detect execution errors, not compilation ones. Don't deploy code with syntax errors.

  • function window.confirm(){ is a MS idiom you must avoid. You may use window.confirm = function() { or, if you're in the global scope, simply var confirm = function() {.

like image 33
Denys Séguret Avatar answered Jun 14 '26 19:06

Denys Séguret