简体   繁体   English

javascript eval()在php文件中不起作用

[英]javascript eval() doesn't work in php file

The function in script section works very well but when i change it to file.php and open it on a server, the eval function doesn't work. 脚本部分中的功能很好用,但是当我将其更改为file.php并在服务器上将其打开时, eval功能不起作用。

<?php 
echo "
<!DOCTYPE html>
<html>
<body>

<button onclick=\"myFunction()\">Try it</button>

<script>
function myFunction() {
    eval(\"alert('it worked')\") ;
}
</script>

</body>
</html>
";//end of echo
?>

Pay more attention. 更加注意。

eval(\"alert('it worked')\")

Must be coded: 必须编码:

  eval("("+alert('it worked')+")").

The bracket "(" can make the code as a object instead of statement. 方括号“(”可以使代码成为对象而不是语句。

Some server may disabled the eval function for security reason, 某些服务器出于安全原因可能会禁用eval功能,

Try to check whether eval function is enabled or not by making the following code 通过执行以下代码,尝试检查eval功能是否已启用

<?php
if(function_exists('eval')) {
echo "eval function is enabled";
}
else {
echo "eval function is not enabled";
}
?>

If you got the else part in the code, this would be reason for not executing your code. 如果您在代码中包含了else部分,这就是不执行代码的原因。

Note: Your code works perfectly works for me. 注意:您的代码对我来说非常有效。 so I dont think there is issue in your code. 所以我认为您的代码中没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM