Link to home
Start Free TrialLog in
Avatar of mraible
mraible

asked on

New Line in Javascript Alert

I'm writing out some javascript code in my
servlet, and if I put a "\n" into my code -
I get a undetermined string error.  If I take
it out, it works fine.

Here's sample code:

 public void doGet (HttpServletRequest request, HttpServletResponse response)
                                                                      throws ServletException, IOException{
             PrintWriter      out;         
       // set content type and other response header fields first
             response.setContentType("text/html");
           // then write the data of the response
       out = response.getWriter();

             // html text ........................

            out.println("<SCRIPT language=\"JavaScript\">");
            out.println("<!--");
            out.println("function Warning(war, war1){");
            out.println("if(war == \"\" || war1 == -1) {");
     ??? out.println("alert(\"1. Save As \n 2.Open \" );");
            out.println("return (false);");
       out.println("}");
       out.println("else {");
      out.println("return (true);");
      out.println("}  }");
          out.println("//-->");
      out.println("</SCRIPT>");

            // html text.........................
       }
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You must escape the backslash with a backslash, otherwise the newline character is written to the alert statement, instead of the text that the alert is supposed to display.  For example, your code above would output this:

 alert("1. Save As
 2. Open");


instead of:

 alert("1. Save As \n2. Open");