I am generating a javascript alert box from codebehind in asp.net(vb).
The code:
Catch ex As Exception
MesgBox(\"Error in up
JavaScript does not allow string constants to span line breaks.
In your server-side code, you're going to have to replace the newlines in the error message with "\n".
Add Imports System.Web.Script.Serialization
to the top of your file, then try this:
Private Sub MesgBox(ByVal sMessage As String)
Dim serializer as New JavaScriptSerializer()
Dim msgedtble As String = serializer.Serialize(sMessage)
Page.ClientScript.RegisterStartupScript(Me.GetType, "myScripts",
"<script type='text/javascript'>alert(" & msgedtble & ");</script>")
End Sub
Using JavaScriptSerializer should take care of the linebreaks, single quotes, and everything else we haven't already thought of.
To carry on with the answer given by Pointy, your message contains '
characters, which you need to escape.
Try this...
msgedtble = msgedtble.Replace(vbNewLine, "\n").Replace("'","\'")
It is also better to have type='text/javascript'
rather than language='javascript'
... although this is not absolutely necessary.