dousi7919
dousi7919
2011-09-19 17:15

jQuery问题从PHP脚本接收html变量

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("&lt;p&gt;Fundada em 2000 e inserida no &lt;strong&gt;Grupo CIL&lt;/strong&gt;, a CilNet &amp;eacute; uma empresa de Servi&amp;ccedil;os de Engenharia na &amp;aacute;rea das Tecnologias de Informa&amp;ccedil;&amp;atilde;o, com compet&amp;ecirc;ncias em Redes de Comunica&amp;ccedil;&amp;atilde;o de Dados, Voz e V&amp;iacute;deo.&lt;/p&gt;
&lt;p&gt;Tendo como base uma larga experi&amp;ecirc;ncia no mercado nacional, a CilNet assume-se como um parceiro tecnol&amp;oacute;gico no sector empresarial, com especializa&amp;ccedil;&amp;atilde;o em solu&amp;ccedil;&amp;otilde;es tecnol&amp;oacute;gicas pioneiras a n&amp;iacute;vel mundial.&amp;nbsp;&lt;/p&gt;");

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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doupin8555 doupin8555 10年前

    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:

    <?php
        // Code to create $content var here //
        $content = addSlashes($content);
    ?>
    
    <script>
        <![CDATA[
            addBox('<?php echo $content; ?>');
        ]]>
    </script>
    

    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!

    点赞 评论 复制链接分享

为你推荐