3

heres my code :

<td onclick="openFile('<?php echo htmlentities ($row['name'])?>','
                      <?php echo htmlentities ($row['content'])?>')">
    <a href="#nf" data-toggle="tab"><?php echo $row['name']?></a></td>

as there is an endline char in $row['content']:

openFile('wow','wow wow

wow ');

and when browser cant find an ending qoutes in the same line it throws an

error(SyntaxError: unterminated string literal [Break On This Error]).

Any solution for this ?

2
  • Do you need the line break in that string? If not, you can just remove it with a simple replace. Commented Dec 4, 2012 at 18:31
  • i need that line break so cant replace it Commented Dec 4, 2012 at 18:33

2 Answers 2

8

You can remove it by replacing it with nothing,

str_replace("\n","",$row['name']);

If you need your line break to be in your JS string you can double escape it,

str_replace("\n","\\n",$row['name']);
Sign up to request clarification or add additional context in comments.

1 Comment

<?php echo str_replace("\n","\\n",htmlentities ($row['content']));?> it worked for me , thanx alot
0

I have the same issue like this before. json_encode in PHP and JSON.parse() in JS helped me resolve this. In your case, I would use this code:

<td onclick="openFile('<?php echo htmlentities ($row['name'])?>','
                  <?php echo json_encode($row['content'])?>')">
<a href="#nf" data-toggle="tab"><?php echo $row['name']?></a></td>

Then on your openFile() function, that is where Im gonna parse content parameter using JSON.parse() like this:

function openFile(name, content){
     content = JSON.parse(content);

     ....
     // your codes here

}

Comments

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.