Question

I'm stuck again with alertboxes!

This works like a charm:

Response.Write("<script>alert('" + "Hello"+ "');</script>");

result is a string. I need to display that. This is where my problem comes in. This doesn't popup:

Response.Write("<script>alert('" + result + "');</script>");

I am passing result across many layers. Also I have used this format in all pages. Don't want a message box. Or a modal popupwindow. I want this. I checked online and according to the syntax, it is supposed to work! Please help!

Was it helpful?

Solution

To put any value in a Javascript string literal, you need to escape apostrophes (as they are used as the delimiter for the string) and backslashes:

Response.Write(
  "<script>" +
  "alert('" + result.Replace("'", "\\'").Replace("\\", "\\\\") + "');" +
  "</script>"
);

OTHER TIPS

Try this Response.Write("<script type='text/javascript'>alert('"+result+"')</script>")

Try this

String message = String.format({0}{1}{2},
  "<script type='text/javascript'>alert('",
  result,
  "')</script>");

Response.Write(message);
Response.Write("<script> alert('" + HttpUtility.HtmlEncode(result) + "'); </script>")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top