Ever get the feeling that software companies add bugs just to mess
with you? This is one of those types of things. I've known about
it for a while, but one of our visitors just reminded me of it and
I thought I'd take this opportunity to share the weirdness.
This script works perfectly:
<%
Response.Write("<script language=""javascript"">" & vbCrLf)
Response.Write("<!--" & vbCrLf)
Response.Write("alert('The time at the server is: ")
Response.Write(Time() & "');" & vbCrLf)
Response.Write("//-->" & vbCrLf)
Response.Write("</script>" & vbCrLf)
%>
This one (the same script only in script tags instead of <% %>) doesn't work:
<script language="VBScript" runat="server">
Response.Write("<script language=""javascript"">" & vbCrLf)
Response.Write("<!--" & vbCrLf)
Response.Write("alert('The time at the server is: ")
Response.Write(Time() & "');" & vbCrLf)
Response.Write("//-->" & vbCrLf)
Response.Write("</script>" & vbCrLf)
</script>
It throws an error complaining about nested script tags.
The tag can be inside a string or even in a comment...
it won't work either way! Okay... easy fix... I'll
split the script tags so the compiler doesn't recognize them:
<script language="VBScript" runat="server">
Response.Write("<scr" & "ipt language=""javascript"">" & vbCrLf)
Response.Write("<!--" & vbCrLf)
Response.Write("alert('The time at the server is: ")
Response.Write(Time() & "');" & vbCrLf)
Response.Write("//-->" & vbCrLf)
Response.Write("</scr" & "ipt>" & vbCrLf)
</script>
Wouldn't you think that would take care of it? Well it doesn't...
now it somehow decides that it's going to interpret the client side
comment before the asp code and sends any ASP code(!) between the comment
tags to the browser as text! That's right it sends your ASP source code
to the client because it saw a client side HTML comment tag!
Don't ask me why... I don't know and I don't want to know!
This one works:
<script language="VBScript" runat="server">
Response.Write("<scr" & "ipt language=""javascript"">" & vbCrLf)
Response.Write("<!-" & "-" & vbCrLf)
Response.Write("alert('The time at the server is: ")
Response.Write(Time() & "');" & vbCrLf)
Response.Write("//-" & "->" & vbCrLf)
Response.Write("</scr" & "ipt>" & vbCrLf)
</script>
No wonder people never use the script tags... <% %> all the way! ;)
Thanks to Claude Schrader for reminding me about this little quirk.
If you have a tip you would like to submit, please send it to:
webmaster@asp101.com.