1

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;
}
7
  • What is addBox doing? Definitely smells like an encoding issue. Commented Sep 19, 2011 at 17:17
  • problem is in the addBox call not in the function itself... Commented Sep 19, 2011 at 17:24
  • Did you try decoding your Html - opinionatedgeek.com/dotnet/tools/htmlencode/decode.aspx Commented Sep 19, 2011 at 17:44
  • Please provide some addBox pseudo code. Commented Sep 19, 2011 at 17:47
  • Kris, decode will only return more conflict chars like ' or " no?? I tried encoding and no luck. the function addBox is on top although it returns the error always before Commented Sep 19, 2011 at 17:52

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the repply! unfortunatly the problems persists... changed it to $content=addslashes($row->content); $this->out.='<script> addBox("'.$content.'"); </script>';
Hey dSquared!! Thanks for the tip. At first I was getting an "unexpected token: <" then I putted all in the same line like: //<![CDATA[addBox("'.$content.'");//]]> and I got rid of that error. But it still doesn't work... Now it seems to break in new lines :/
Hi!! Had to adapt your answer for the newlines and some older browser compatibility. I'm editing your answer to place the final code and accepting it! Thanks once again ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.