jQuery 从 PHP 脚本接收 html 变量时出现问题
我通过 php 将其内容发送到 javascript 来生成一堆 tinymce 编辑框。
我正在做类似的事情
<script>
addBox('<?$content?>');
</script>
问题是每次发送的文本有一个“/”字符时,该函数就会被破坏并返回一个错误,例如:
Uncaught SyntaxError: Unexpected token ILLEGAL
我发现它至少用这个字符返回此错误...不知道是否这会发生在其他人身上。该函数在调用时出现错误,例如:
addBox("<p>Fundada em 2000 e inserida no <strong>Grupo CIL</strong>, a CilNet &eacute; uma empresa de Servi&ccedil;os de Engenharia na &aacute;rea das Tecnologias de Informa&ccedil;&atilde;o, com compet&ecirc;ncias em Redes de Comunica&ccedil;&atilde;o de Dados, Voz e V&iacute;deo.</p>
<p>Tendo como base uma larga experi&ecirc;ncia no mercado nacional, a CilNet assume-se como um parceiro tecnol&oacute;gico no sector empresarial, com especializa&ccedil;&atilde;o em solu&ccedil;&otilde;es tecnol&oacute;gicas pioneiras a n&iacute;vel mundial.&nbsp;</p>");
有人可以帮忙吗?
addBox的代码如下:
function addBox(text){
elem = "txt" + window.counter;
var tiny = $.ajax({
type: "POST",
url: "inc/ajax.php?act=inserebox",
data: "value=txt" + window.counter + "&text="+encodeURIComponent(text),
async: false
}).responseText;
$('.more_boxes').append(tiny);
//$(tiny).append('.more_boxes');
tinyMCE.init({
url:'../js/tinymce/jscripts/tiny_mce/plugins/ibrowser',
mode:"exact",
elements: elem,
theme:"advanced",
height:"220",
entity_encoding : "raw",
plugins : "safari,pagebreak,style,table,advimage,advlink,emotions,iespell,media,searchreplace,print,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,inlinepopups,ibrowser",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : false
});
window.counter+=1;
return true;
}
I'm generating a bunch of tinymce edit boxes by sending it's content to javascript through php.
I'm doing something like
<script>
addBox('<?$content?>');
</script>
The problem is that everytime the text sent has a "/" character the function is broke an returns an error like:
Uncaught SyntaxError: Unexpected token ILLEGAL
I found it to return this error at least with this char... Don't know if it'll happen with others. The function is giving error when called like:
addBox("<p>Fundada em 2000 e inserida no <strong>Grupo CIL</strong>, a CilNet é uma empresa de Serviços de Engenharia na área das Tecnologias de Informação, com competências em Redes de Comunicação de Dados, Voz e Vídeo.</p>
<p>Tendo como base uma larga experiência no mercado nacional, a CilNet assume-se como um parceiro tecnológico no sector empresarial, com especialização em soluções tecnológicas pioneiras a nível mundial. </p>");
Can anyone help?
The code for addBox is as follows:
function addBox(text){
elem = "txt" + window.counter;
var tiny = $.ajax({
type: "POST",
url: "inc/ajax.php?act=inserebox",
data: "value=txt" + window.counter + "&text="+encodeURIComponent(text),
async: false
}).responseText;
$('.more_boxes').append(tiny);
//$(tiny).append('.more_boxes');
tinyMCE.init({
url:'../js/tinymce/jscripts/tiny_mce/plugins/ibrowser',
mode:"exact",
elements: elem,
theme:"advanced",
height:"220",
entity_encoding : "raw",
plugins : "safari,pagebreak,style,table,advimage,advlink,emotions,iespell,media,searchreplace,print,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,inlinepopups,ibrowser",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : false
});
window.counter+=1;
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PHP 的内置 addslashes 在非法字符传递到之前对其进行转义小盒子。在将 $content 变量传递给 JS 脚本之前,您需要对其执行此操作。
编辑:
尝试将解码的 HTML 和 addSlashes 组合起来,如下所示:
如果您没有用
括住 Javascript,那么如果角度会出现错误找到括号,因为它会被解释为 HTML 标记的开头。
希望这有帮助!
You can use PHP's built-in addslashes to escape illegal characters before they get passed to the tinymce box. You will need to do this to the $content var before passing it to the JS script.
EDIT:
Try a combination of decoded HTML and addSlashes like this:
If you don't enclose your Javascript with
<![CDATA[]]>
, then you'll get errors if angle'd brackets are found, because it'll be interpreted as the start of an HTML tag.Hope this helps!