I was just writing some simple code and I noticed that using document.writeln doesn't write to a new line, permit me to demonstrate...
// this is my JSON object
var myObject = {
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": [{
"Address1": "11 My Street",
"Address2" : "Nice Area",
"Town" : "Nice Town",
"PCode" : "P05T 0DE"
}]
}
document.writeln(myObject.firstName);
document.writeln(myObject.address[0].Address1);
now this outputs the following....
John 11 My Street
It's all on one line? If I used document.write() I'd expect this? It's happening in both IE & Firefox. Obviously I could add + "<br/>"
or + "\n"
but I shouldn't need to do that?
Am I being stupid?
Use the \n for a newline character. document. write("\n");
The Difference Between write() and writeln()writeln() adds a newline character after each statement. write() does not.
The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.
The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems. The <br> tag is an empty tag which means that it has no end tag.
view the source of the page to see how the browser puts each line on a line-
but if you want to see a newline in the html, yes, put a '<br>'
between them.
document.writeln
does this:
Writes a string of text followed by a newline character to a document.
But whitespace is collapsed and converted to a single space when rendering HTML so your newline does nothing inside the HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With