简体   繁体   English

如何处理JavaScript函数参数中的非法字符

[英]How to deal with illegal characters in javascript function parameters

I have a javascript function that I am calling in my code: 我在代码中调用了一个javascript函数:

<script type="text/javascript">
    opener.postConditionPostCure(
        "<?php echo get_field('cure_description'); ?>"
    );
</script>

The issue is that I do not know what will be echoed by the php. 问题是我不知道php将回显什么。 I just ran into an issue where the javascript is failing because the php is echoing an illegal token, in this case the "(". How can this issue be dealt with. Thanks. 我只是遇到了一个JavaScript失败的问题,因为php正在回显非法令牌,在这种情况下为“(”。如何解决此问题。谢谢。

一个可取的事情是确保php返回规范化的数据,也许是JSON字符串。

I hate having php intermingled with my js makes it hard to read the js I prefer to declare it as variables at the top of my js code if possible (and it usually is) 我讨厌将php与我的js混合在一起,使得很难阅读我的js,如果可能的话,我更喜欢将其声明为js代码顶部的变量(通常是这样)

<script type="text/javascript">
    var description = <?php echo json_encode(get_field('cure_description')); ?>;

    opener.postConditionPostCure(description);
</script>

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

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