Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Newline character doesn't render in browser  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the below code displays new line in console, but not in the browser/database? How can I resolve this?



Please assume that this code snippet is a part of the bigger code that is saving the conversation in a database.
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because newline characters are normally interpreted by HTML as spaces. The usual way to have the text start on the next line in HTML is via a <br> tag.

(If the text is enclosed in a <pre> element then newline characters are interpreted as new line instructions, but <pre> does other things which might be inconsistent with what you want the page to look like.)
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1 Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, you can't send html tags in a stringbuilder, they're not being rendered eather, if you use , that's exactly how it will show up, as a string, not as an htmltag
 
Saloon Keeper
Posts: 5344
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If I remember correctly, you can't send html tags in a stringbuilder, they're not being rendered eather, if you use , that's exactly how it will show up, as a string, not as an htmltag


It's immaterial how the HTML that gets sent to the browser is created. If HTML tags are being sent, they will be interpreted. If they show up as string then something else is at work that prevents them from being interpreted.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked for it, but coudn't find the post anymore, but I seem to remeber I posted the same question once, I also was using a stringbuilder to build a message and at the end return it to the model.
Neather \n, \r, \n\r nor br worked
 
Tim Moores
Saloon Keeper
Posts: 5344
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again: Using or not using a StringBuilder makes no difference whatsoever. Something else must have been the reason.

Paul has already pointed out that all whitespace will be treated as a single space.
 
Saloon Keeper
Posts: 10001
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML only uses whitespace as a token delimiter. That means that any whitespace (besides spaces between words) that you want to have in the web page must be in the form of HTML tags. As has been said, line breaks are inserted into a text span with the <br /> tag. If this shows up in the web page as the literal text " <br />", then it's likely because you're trying to inject it into a template that is being processed by a template engine, which will escape anything that looks like HTML. Depending on the template language, there are different ways of injecting literal HTML.
 
Marshal
Posts: 67255
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As previously stated, newlines are just another whitespace character which HTML treats as a delimiter and will not render as anything in the output. Use HTML elements and CSS to control layout.

HTML can be built up in strings and still be interpreted as HTML.

There's lots of context missing from the original question. Why are you building HTML in a string in the first place? Is this a Java web application? If so, you should likely be building the view in a JSP. If not, how is the HTML string getting to the browser?
 
Bartender
Posts: 20580
121
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general rule, you should not attempt to create HTML in server-side logic. It's far better to use some sort of View Template (for example, a JSP or a JSF xhtml) that contains the HTML and insert the variable data into that template for display.

As everyone else has said, "space" characters such as space, tab, newline and the like are treated specially in HTML. First, because all of them render simply as a blank horizontal space - and that includes newline. Secondly, because any sequence of "space" characters renders simply as a single horizontal space, whether there's 1 or 101. To actually render multiple horizontal space characters, there's something called the non-break space entity: &nbsp;, but actually that's not a very good thing to use. Spacing control is better done with layout tags such as <div> and with CSS.

OK, so much for Yet Another HTML lesson. If you're actually trying to, say, capture and display a chat conversation verbatim, there's a way to honor how text was originally spaced and lined. Use the <pre> tag to wrap it.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!