[Javascript] String on multiple lines

Peter Lauri lists at dwsasia.com
Wed Jun 21 00:13:13 CDT 2006


This did it, but with PHP :)

No Javascript solution?

-----Original Message-----
From: javascript-bounces at LaTech.edu [mailto:javascript-bounces at LaTech.edu]
On Behalf Of Paul Novitski
Sent: Tuesday, June 20, 2006 5:45 AM
To: JavaScript List
Subject: Re: [Javascript] String on multiple lines

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 

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list