By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

inporting many lines of HTML through javascript

P: 200
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;
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?
Jul 20 '09 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 5,390
you may use concats like:

Expand|Select|Wrap|Line Numbers
  1. var foo = 'first line'
  2.     + 'second line'
  3.     + 'third line';
Expand|Select|Wrap|Line Numbers
  1. var foo = [
  2.     'first line',
  3.     'second line',
  4.     'third line'
  5. ].join('');
kind regards
Jul 20 '09 #2

P: 126
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 ;)
Jul 23 '09 #3

Expert Mod 5K+
P: 8,639
you can also escape the newline:
Expand|Select|Wrap|Line Numbers
  1. var msg = "some very \
  2. long text";    
though it won’t preserve the line break.
Jul 23 '09 #4

P: 126
Never knew this was possible, well we learn something new everyday... ;)
Jul 23 '09 #5

Expert Mod 5K+
P: 8,639
I just stumbled upon that recently…
Jul 23 '09 #6

rnd me
Expert 100+
P: 427
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.
Jul 25 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.