SyntaxError:非法的返回语句-正在url中搜索字符串

SyntaxError: Illegal return statement - searching for string in url

本文关键字:url 正在 搜索 字符串 语句 非法 返回 SyntaxError      更新时间:2023-09-26

我现在只是在Chrome的控制台中。我创建了这个变量:

url = window.location.href

它给出:

url"http://www.example.com/trips/dest/australia-and-south-pacific/cntry/fiji/"

现在我尝试了这个:

if (url.indexOf('australia-and-south-pacific') > 0) {return 'foo';}

返回:

SyntaxError: Illegal return statement

我希望控制台返回"foo"。

为什么会发生这种情况?我注意到,在收到这个错误后,给它几秒钟,就会出现一个新的错误:

Uncaught TypeError: Cannot read property 'hide' of undefined 

这是否有助于诊断?

根据EMCAScript语言规范,

如果ECMAScript程序包含一个不在FunctionBody中的返回语句。