[Javascript] String on multiple lines

Paul Novitski paul at juniperwebcraft.com
Mon Jun 19 17:45:17 CDT 2006


At 09:50 AM 6/19/2006, Peter Lauri wrote:
>I have a variable in PHP that is like $variable. 
>The content of this is separated with \n to 
>linebreaks. I want to use this and pass it to 
>the Javascript that I have so I do something like this:
>
>
.javascript stuff

>
>function JavaScriptFkn() {
>             var variable = “<?php echo $variable; ?>”;
>}
>
>
. Javascript stuff
.
>
>That ends up to be like this:
>
>function JavaScriptFkn() {
>             var variable = “The first row

><p>The second row
</p>
><h1>The third row</h1>”;
>}
>
>As you see, the row is split up, and the 
>javascript will not see the rows. Is there any 
>way of getting around this? I know I can 
>probably solve this in PHP by taking away the \n 
>that is destroying my day. Anyone with a simple and workable solution?


Peter,

I'd try escaping the PHP line breaks, replacing 
\n with \\n, so that they output into your HTML 
(JavaScript) source as the literal string "\n" 
instead of the line-break character.

         var variable = “<?php echo str_replace("\n", "\\n", $variable); ?>”;

This should output:

         var variable = “The first row...\n<p>The 
second row
</p>\n<h1>The third row</h1>”;

Regards,

Paul 




More information about the Javascript mailing list