Connecting Tech Pros Worldwide Help | Site Map

inporting many lines of HTML through javascript

mikek12004's Avatar
Familiar Sight
 
Join Date: Sep 2008
Location: Athens, Greece
Posts: 186
#1: Jul 20 '09
I want to do this

Expand|Select|Wrap|Line Numbers
  1. msg='
  2. <div> 
  3.   mike
  4. </div>
  5. ';
  6. document.getElementById("sme_cont").innerHTML=msg;
  7.  
The problem is that the msg variables sees the '\n' character and prompts and error. If I put all the HTML code of the variable in one line works OK but inside the msg variable I plan to put complex HTML code so the ability to put it in multiple line is essential how can I do this?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,126
#2: Jul 20 '09

re: inporting many lines of HTML through javascript


you may use concats like:

Expand|Select|Wrap|Line Numbers
  1. var foo = 'first line'
  2.     + 'second line'
  3.     + 'third line';
  4.  
or:
Expand|Select|Wrap|Line Numbers
  1. var foo = [
  2.     'first line',
  3.     'second line',
  4.     'third line'
  5. ].join('');
kind regards
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#3: Jul 23 '09

re: inporting many lines of HTML through javascript


There's another workaround, if you use like Notepad++ or PSPAD, you could use a single line HTML in a variable and replace the "\n" to let's say "<!--NEWLINE--> in your HTML. So whenever you need to work in it you just replace the <!--NEWLINE--> to "\n" and the opposite. This is not the best solution, but it works ;)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Jul 23 '09

re: inporting many lines of HTML through javascript


you can also escape the newline:
Expand|Select|Wrap|Line Numbers
  1. var msg = "some very \
  2. long text";    
  3.  
though it won’t preserve the line break.
Canabeez's Avatar
Member
 
Join Date: Jul 2009
Location: Israel
Posts: 85
#5: Jul 23 '09

re: inporting many lines of HTML through javascript


Quote:

Originally Posted by Dormilich View Post

you can also escape the newline:

Expand|Select|Wrap|Line Numbers
  1. var msg = "some very \
  2. long text";    
  3.  
though it won’t preserve the line break.

Never knew this was possible, well we learn something new everyday... ;)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Jul 23 '09

re: inporting many lines of HTML through javascript


I just stumbled upon that recently…
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#7: Jul 25 '09

re: inporting many lines of HTML through javascript


if you have lots of text, a hidden input or textarea lets you store it without encoding. in a script file, you have to encode it.

i wrote a couple firefox batches to help.
just go to the link, click the "x" tab on the left.
then paste in your text, click "run", and click back on the "y" tab to view the results.

this string quoter one converts text into this format:
Expand|Select|Wrap|Line Numbers
  1. "dddd"+
  2. "eeee"+
  3. "gggg"
this slash adder produces the format shown by canabeez.

the JSONString version is an old standby, though it makes it hard to read.
Reply