Loans - Favorite - Neopets Cheats, Games and Neopoints - Myspace Images - GBA Roms

PDA

View Full Version : Cannot create newline with alert box


chuckdawit
Jun 8th 2007, 9:28 pm
I can't separate my strings onto new lines using the alert box. I want to separate my phone numbers onto new lines and then print them in the alert box like so:

<A HREF='javascript:(function(){
var s = "";
for (var i = 0; i < phoneArray.length; i++) {
s += phoneArray[i] + "\n";
}
alert(s);
} ) ()'>Phone Numbers</A>

I've tried every combination I can think of (single, dubble quotes, \n, \r, etc) nothing will split them in the result string. And I reload the page every time. Do I need to do something else?

ajsa52
Jun 9th 2007, 1:14 am
Probably you're getting some javascript error. Check your javascript console.
Working example using letters and numbers:

<html>
<body>
<a href="javascript:alert('a'+'\n'+2+'\n'+'b');">test</a>
</body>
</html>

krt
Jun 9th 2007, 2:03 am
So you are getting an alert box with no new lines between the phone numbers? I tried your code in the address bar and it works fine (provided I use dummy data), e.g.: javascript:var s = ""; for (var i = 0; i < 3; i++) { s += "test" + "\n"; } alert(s);
Copy paste that into your address bar in your web browser, (press enter) and tell me if you get the new lines. If so, can you show more of the code. If not, what browser are you using.

chuckdawit
Jun 9th 2007, 10:49 am
I had to put another \ character in front of the string like this: "\\n". Now it works. Can anyone explain why it needs two \ chars?
-Chuck

ajsa52
Jun 9th 2007, 10:55 am
You need two \\ when you are generating javascript code from another language, to achieve the '\' character on new source (javascript).