我正在试图找到此代码中的错误。我已经多次检查过,有人可以指出问题所在吗?
$(function() {
try {
function endswith(str, ends) {
if (ends === '') return true;
if (str == null || ends == null) return false;
str = String(str);
ends = String(ends);
return str.length >= ends.length && str.slice(str.length - ends.length) === ends;
}
var referrer = new URL(document.referrer).domain;
if (endswith(referrer, "xyz.com")) {
$(".logo .logo-external").remove();
} else {
$(".logo .logo-internal").remove();
}
} catch () {}
});
答案 0 :(得分:7)
catch (e) {}
你错过了变量e
$(function() {
try {
function endswith(str, ends) {
if (ends === '') return true;
if (str == null || ends == null) return false;
str = String(str);
ends = String(ends);
return str.length >= ends.length && str.slice(str.length - ends.length) === ends;
}
var referrer = new URL(document.referrer).domain;
if (endswith(referrer, "xyz.com")) {
$(".logo .logo-external").remove();
} else {
$(".logo .logo-internal").remove();
}
} catch (e) {}
});
答案 1 :(得分:0)
根据MDN,try...catch
语法的定义类似于以下内容:
try {
try_statements
}
...
[catch (exception_var) {
catch_statements
}]
[finally {
finally_statements
}]
这意味着exception_var
不是可选的。否则,它看起来像这样:
...
[catch ([exception_var]) { // Uncaught SyntaxError: Unexpected token )
catch_statements
}]
...