PDA

View Full Version : How to use linebreaks in arrays



prem_amrit
Sep 13th, 2007, 10:28 PM
I'm trying to get this to work, but well, I'm not that good, or else I'm too tired to get the problem.

When I use this code it is suppose to be linebreaks, but they don't work.

Also, is there any way to parse HTML-syntax inside variables?
Actually I would like to have the first line bold and the other to lines stays unchanged.

Anyhow, I tried this for a few hours now and well, my brain won't go further right now.



var beforeMsg = "<strong>";
var afterMsg = "</strong";
var msgRotateSpeed = 5000; // Rotate delay in milliseconds
var textStr = new Array();
textStr[0] = "First bolded line"+
"\nSecond line"+
"\nThird line";
textStr[1] = "Second text";
textStr[2] = "Third text";
textStr[3] = "Fourth text";
if (document.layers) {
document.write('<ilayer id="NS4message" height=25 width=100%><layer id="NS4message2" height=25 width=100%></layer></ilayer>')
temp = 'document.NS4message.document.NS4message2.document.write(beforeMsg + textStr[t++] + afterMsg);'+
'document.NS4message.document.NS4message2.document.close()';
}
else if (document.getElementById) {
document.write(beforeMsg + '<div id="message">&nbsp;</div>' + afterMsg);
temp = 'document.getElementById("message").firstChild.nodeValue = textStr[t++];';
}
else if (document.all) {
document.write(beforeMsg + '<div id="message">&nbsp;</div>' + afterMsg);
temp = 'message.innerHTML = textStr[t++];';
}
var t = 0;
function msgRotate() {
eval(temp);
if (t == textStr.length) t = 0;
setTimeout("msgRotate()", msgRotateSpeed);
}
window.onload = msgRotate;

PremiumBlend
Sep 14th, 2007, 03:32 AM
View your source code. Do you see the \n's in it? They should be there, but they don't create a line break in a browser, a <br /> does. Try replacing the \n with <br /> and see if that works.

prem_amrit
Sep 14th, 2007, 01:33 PM
Hi there premium blend.

Well. I checked the syntax for "Special string characters" and they state:
\n
A newline character. Use this if you want to insert a line break into text.
\f
A form feed. Try not to use this ever. Use \n instead.
\r
A carriage return. Try not to use this ever. Use \n instead.
\t
A tab character.
\\
A \ character
\/
A / character

And since I can't use <br /> within variables I'm sort of stuck.
If I write them all that happens is that they get displayed as regular text.

Arbitrator
Sep 14th, 2007, 02:16 PM
And since I can't use <br /> within variables I'm sort of stuck.
If I write them all that happens is that they get displayed as regular text.You can preserve literal line‐breaks by using the CSS2.1 declaration white-space: pre-line. Since thatís currently poorly supported, the white-space: pre declaration, which also prevents whitespace collapsing, would have to be used instead.

You might also want to follow Windows line‐breaking convention and use \r\n for each line‐break. I vaguely recall having an issue with Internet Explorer not breaking a line in the past when I only used \n.

Below is an example of the first part of your code rewritten. Your original code seems to exhibit some poor practice: using the inflexible document.write; using the inefficient eval; declaring a variable (var t) after code that itís called by; using the No‐Break Space (&nbsp;) character as a placeholder; and writing code thatís invalid HTML 4/XHTML 1 (strong cannot contain block‐level elements like div). I also donít know why youíre catering to dead browsers like Netscape 4 and the ancient versions of Internet Explorer that require document.all.


var d = document;
var textStr = [
"First bolded line\r\nSecond line\r\nThird line",
"Second text",
"Third text",
"Fourth text"
];
var div = d.createElement("div");
var strong = d.createElement("strong");
strong.appendChild(d.createTextNode(textStr[t++]));
div.appendChild(strong);
// Insert the non‐document.write code for inserting the ďdivĒ element into the document here.

LFCFan
Sep 14th, 2007, 02:41 PM
Arbitrator you scare me lol

prem_amrit
Sep 16th, 2007, 12:26 PM
Hi there Arbitrator.

I'm more then new to this kind of coding, so sometimes I fetch some free script online and sometime they work, and sometimes they don't.

However, it forces me to find my answers, and that I like. :)

So many thanks to you. :)