Forum Moderators: open

Message Too Old, No Replies

\n and alert window

shows the \n and not a new line

         

humpg

3:05 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



I have an asp page that sends back data:

Response.ContentType = "text/plain"

Response.Write("E,An error occurred while trying to save your results.\nYour results have not been recorded for this test.\n\nDescription: " & strDescription & "\nError Number: " & strNumber & "\nSource: " & Request.ServerVariables("SCRIPT_NAME"))

Response.End()

And on my page I have the following:

if ( x.substr(0,1) == "E") {
window.alert( x.substr(2) );
}

I get my message in my alert box but it doesn't recognize the \n instead they show up in the alert box.

How do I get it to recognize the \n?

lavazza

7:38 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



I suspect it's the "text/plain"

try

type="text/javascript"

although I suspect that javascript might not recognise the & symbol

and the double-quotes can be a source of bugs

maybe the following will work

Response.Write('E, An error occurred while trying to save your results.\nYour results have not been recorded for this test.\n\nDescription: ' + strDescription + '\nError Number: ' + strNumber + '\nSource: ' + Request.ServerVariables('SCRIPT_NAME'))

humpg

11:47 am on Oct 26, 2007 (gmt 0)

10+ Year Member



Nope,

Tried changing to text/javascript didn't work. Still gave me the same message with no line breaks.

' qoutes are used for comments in asp so that is out, and I need the & to add the variables into the string for asp.

daveVk

12:53 pm on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Think this is ASP issue Try

Response.Write("E,An error occurred while trying to save your results." + vbCRLF + "Your results have etc etc

ps. not ASP expert, but assume \n has no special meaning in ASP

humpg

1:05 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



That was it, thank you very much!

Well, almost the + still need to be & for asp.

;-) Why didn't I think of that.

XtendScott

2:40 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



You can also return JavaScript compliant code and then use eval() on the JavaScript side.

ASP:

Response.Write("x = ""E,An error occurred while trying to save your results.\nYour results have not been recorded for this test.\n\nDescription: " & strDescription & "\nError Number: " & strNumber & "\nSource: " & Request.ServerVariables("SCRIPT_NAME") & """;")

Javascript:

eval(responseText);
if ( x.substr(0,1) == "E") window.alert(x.substr(2));

I was testing with an ajax call which I am assuming is similar to what you were doing.