运行此代码时,我收到一个非常奇怪的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中的非法返回语句
发布评论