JavaScript:SyntaxError:missing)-如何正确地转义php中的js


JavaScript: SyntaxError: missing ) after argument list - how to properly escape js in php?

我很难在php文件中转义这个js函数:它返回错误:JavaScript:SyntaxError:缺少)代码是这样的:

echo "<a href='"'" onClick='"show_confirmation('",$messages['deleting_message'],"', '",$messages['close'],"', 'show_hide('confirmation');', '",$lang['delete'],"', 'redirect_communicator('",$fullurl,"');'); return false;'">Delete</a>";

基本上,这是一个有5个参数的js函数,其中一些也是函数,这是有问题的。像$messages[]这样的所有变量都是语言定义的变量。。该错误显示最后一个参数-函数redirect_communicator()

你能告诉我出了什么问题以及如何摆脱它吗?感谢您的任何建议

编辑:我也试过这个:

<a href="" onClick="show_confirmation('<?php echo $messages['deleting_message'];?>', '<?php echo $messages['close'];?>', show_hide('confirmation');, '<?php echo $lang['delete'];?>', redirect_communicator('sda');) return false;">delete</a>              

仍然是同样的错误。。当我添加json_encode时,出现了另一个错误。。

像这样混合语言总是很难阅读,而且很少是个好主意。

然而,如果你需要这样做,你可以使用sprintf来帮助澄清它:

echo sprintf(
    "<a href='"'" onClick='"show_confirmation('%s', '%s', show_hide('confirmation'), '%s', redirect_communicator('%s')); return false;'">Delete</a>",
    $messages['deleting_message'],
    $messages['close'],
    $lang['delete'],
    $fullurl
);