• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1382
  • Last Modified:

Line Breaks

This is probably pretty easy, I just can't figure it out.  I have a string that I'm reading in from innerHTML... when I look at the string in the source code it's got breaks all over the place.  though when it's in the HTML there's no breaks.

I tried a ...innerHTML.replace("\n", "<br />") but it didn't seem to work.  what's the best way to do this?
0
BeamerCola
Asked:
BeamerCola
  • 13
  • 7
  • 4
  • +1
1 Solution
 
arantiusCommented:
var str=element.innerHTML;
str.replace("/\n/", "<br />");
element.innerHTML=str;

Untested, but I'm quite sure that will work for you.  Let me know =)
0
 
knightEknightCommented:
or:

str = str.split("\n").join("<br />");
0
 
knightEknightCommented:
in any event, you will need to assign the results:

element.innerHTML = element.innerHTML.replace( "\n", "<br />" ) ;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GwynforWebCommented:

<form>
  <p><textarea name="t1" cols="60" rows="10">
This is some text
with a few line breaks

Sometimes more than one
      sometimes with some extra spaces for indentation
And somtines not but space      between words for formatting.
</textarea></p>

<input type="button" value="Add to page"
   onclick="document.body.innerHTML+=t1.value.replace(/\n/g,'<br>').replace(/  /g,'&nbsp;&nbsp;&nbsp;&nbsp;'); ">
<input type="button" value="View HTML" onclick="alert(document.body.innerHTML) "> </p>
</form>
0
 
BeamerColaAuthor Commented:
okay hmm, that didn't seem to work.  Let me explain my situation a little more.

I've got a frameset, and the bottom frame is uneditable, the top frame is though.  So in the top frame I'm using javascript to format the uneditable frame.

Here's the source of the bottom frame:
-----
No problem, I will transfer the additional money into you
r acct today.
 
Thanks.
 
Vicky
-----

and my script:
parent.BODY.document.body.innerHTML = parent.BODY.document.body.innerHTML.replace("/\n/", "<br />");

I think i'm doing it wrong, and I need to replace whatever character the breaks are with a \n character.  Maybe I'm wrong ...
0
 
knightEknightCommented:
.replace("\n", "<br />");
0
 
knightEknightCommented:
instead of:

.replace("/\n/", "<br />");
0
 
BeamerColaAuthor Commented:
still no luck =/
0
 
knightEknightCommented:
is there an error message?
0
 
knightEknightCommented:
your script is in the top frame, and the bottom frame is named "BODY" , correct?
0
 
knightEknightCommented:
and the pages in both frames are loaded from the same domain (server), correct?
0
 
BeamerColaAuthor Commented:
correct
0
 
knightEknightCommented:
are there any HTML tags in the source of the bottom frame?
0
 
BeamerColaAuthor Commented:
<html><body><pre>[message]</pre>

unfortunately the craphead didnt close html or body.
0
 
knightEknightCommented:
yikes, that could cause the problem ...

What do you see if you do this?

alert( parent.BODY.document.body.innerHTML );
0
 
BeamerColaAuthor Commented:
all the text that I'm trying to alter
0
 
GwynforWebCommented:
what you have in the source code and what the innerHTML is are in general  different
0
 
arantiusCommented:
Are the two frames pages loaded from the same domain?  If you are trying to alter a page loaded from another domain, you cannot.  Security based limitations.
0
 
BeamerColaAuthor Commented:
Yeah they're both on the same domain.  Good point though.
0
 
arantiusCommented:
Well I am unsure what exactly is happening here so...

var str=document.frames.frameName.document.body.innerHTML;
str.replace("/\n/", "<br />");
alert(str);
document.frames.frameName.document.write(str);

Replace frameName above with the actual name of the frame.
What is alerted?  What shows up in the frame?
0
 
knightEknightCommented:
why are you enclosing the newline character in frontslashes?
should be:

str = str.replace("\n", "<br />");
0
 
knightEknightCommented:
or:
str = str.split("\n").join("<br />");
0
 
arantiusCommented:
Because I'm juggling too many regex implementations in my head and I never properly tested this post =)  I was thinking this:

str=str.replace(/\n/, "<br />");

Which I just tested working.  Quotes or slashes works, but not both!
0
 
knightEknightCommented:
ah, yes I see now, but it should be:

str=str.replace(/\n/g, "<br />");

:D
0
 
GwynforWebCommented:
please note

THE SOURCE CODE AND INNER HTML ARE NOT IN GENERAL THE SAME!!!!

 Extra spaces are stripped by the interpreter, else this would have had done the task http:Q_21217645.html#12658579
0
 
BeamerColaAuthor Commented:
yes yes yes KnightEknight!  Thanks!
0
 
knightEknightCommented:
thanks, but it was a group effort.  :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 7
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now