admin管理员组文章数量:1323336
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?
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?
Share Improve this question edited Nov 7, 2012 at 16:11 Ry-♦ 225k56 gold badges492 silver badges498 bronze badges asked Nov 7, 2012 at 16:06 AndrewAndrew 4705 silver badges18 bronze badges 6-
1
Er... you can't catch syntax errors. How about fixing it instead?
window.confirm = function(str) {...};
– Ry- ♦ Commented Nov 7, 2012 at 16:08 -
People aren't going to like me for pointing out that you can catch syntax errors if you parse the code with
eval()
. That would be a terrible idea in this case however because that is a syntax error and it should just be fixed. – Pointy Commented Nov 7, 2012 at 16:09 -
2
@Pointy: Eh, I'd count that as "catching a
SyntaxError
" as opposed to "catching a syntax error". – Ry- ♦ Commented Nov 7, 2012 at 16:10 - @minitech Ha ha good point :-) It's not something I'd normally bring up but I've been working with a JavaScript templating system lately and now I have to deal with caught "SyntaxErrors" all the time. – Pointy Commented Nov 7, 2012 at 16:18
- @minitech - Thanks for the edit. I know it was a syntax error, but I didn't know how to fix it. It worked fine in IE which is why I was just trying to catch it for the other browsers so nothing broke. everyoneelse - Thank for for the knowledge that try/catch only catches runtime, I wasn't aware of that. – Andrew Commented Nov 12, 2012 at 16:50
4 Answers
Reset to default 3function window.confirm(str)
and var window.confirm ...
are invalid. instead, try:
window.confirm = function(str){
..... code here .....
}
Two points :
try/catch
are used to detect execution errors, not pilation ones. Don't deploy code with syntax errors.function window.confirm(){
is a MS idiom you must avoid. You may usewindow.confirm = function() {
or, if you're in the global scope, simplyvar confirm = function() {
.
If you absolutely need to try and catch "Uncaught SyntaxError" errors, then you'll have to rely on JavaScript's hidden-under-the-floorboard misunderstood stepchild eval()
function.
In the sample code below, obj
will only be updated if the ining string evaluates to a valid object; otherwise obj
will default to {}
. Let's assume $someTextField
pulls in settings entered in a CMS textarea field, and the content editor has entered this malformed string in the field:
{
"foo": "bar
}
Sample code:
var oSettings = {};
// Returns "Uncaught SyntaxError: Unexpected token ILLEGAL(…)"
try {
oSettings = <?php echo $someTextField ?>;
} catch(e) {}
// This works
try {
eval('oSettings = <?php echo $someTextField ?>');
} catch(e) {}
Keep in mind that if the string to be evaluated is ing from some external source (e.g., CMS) and you have no control over it as in this example, then you'll need to do three things:
Detect and prevent execution of JavaScript code that could potentially wreck hell on your app.
Escape quotes in the ining string to avoid conflicts with the JavaScript quotes. Since we're using PHP in this example, it would be better to do something like this:
eval('oSettings = <?php echo addslashes($someTextField) ?>');
Remove line breaks since the evaluated string needs to be on one single line. It would be even better to do something like this:
eval('oSettings = <?php echo addslashes(str_replace(array("\r\n", "\n", "\r"), "", $someTextField)) ?>');
You must fix the syntax errors. Try/catch will not catch syntax errors because syntax errors are pile time errors and try/catch can only catch runtime errors.
本文标签: javascriptTry Catch not catching syntax error in ChromeStack Overflow
版权声明:本文标题:javascript - Try Catch not catching syntax error in Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742137260a2422424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论