语法错误:JavaScript中的非法返回语句 [英] Syntax error: Illegal return statement in JavaScript

查看:75
本文介绍了语法错误:JavaScript中的非法返回语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时,我收到一个非常奇怪的JavaScript错误:

I am getting a really weird JavaScript error when I run this code:

<script type = 'text/javascript'>
var ask = confirm('".$message."');
if(ask==false)
{
return false;     
}
else
{
return true;
}
</script>

在JavaScript控制台中它说:

In the JavaScript console it says:

语法错误:非法返回语句

它出现在 return true; 返回false;

(我从php函数回显这个javascript; $ message 变量是php参数之一)

(I am echoing this javascript from a php function; the $message variable is one of the php parameters)

我的代码出了什么问题?

What is wrong with my code?

推荐答案

return 仅在函数内部有意义。您的代码中没有任何功能。

return only makes sense inside a function. There is no function in your code.

此外,如果冗余部门您的代码是值得的。假设你把它移到一个合适的函数,这会更好:

Also, your code is worthy if the Department of Redundancy Department. Assuming you move it to a proper function, this would be better:

return confirm(".json_encode($message).");

稍后编辑:更改代码以使用 json_encode 确保消息内容不会因为消息中的撇号而中断。

EDIT much much later: Changed code to use json_encode to ensure the message contents don't break just because of an apostrophe in the message.

这篇关于语法错误:JavaScript中的非法返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆