语法错误:JavaScript中的非法返回语句

编程入门 行业动态 更新时间:2024-10-21 19:08:27
本文介绍了语法错误: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中的非法返回语句

本文发布于:2023-11-03 15:38:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   语法错误   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!