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='javascriptfunction(){ var s = ""; for (var i = 0; i < phoneArray.length; i++) { s += phoneArray + "\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?
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> Code (markup):
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); Code (markup): 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.
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
You need two \\ when you are generating javascript code from another language, to achieve the '\' character on new source (javascript).