Incomplete JS script tag with RegisterClientScriptBlock

I am currently trying to register a JS function call from a .NET page that just calls a small function on the .aspx page that will close the window.

The RegisterClientScriptBlock code looks like this:

Page.ClientScript.RegisterClientScriptBlock (typeof(Page), "closeBox", "<script type='text/javascript'>closeBox();</script>");

      

This kind of thing works elsewhere in the application on different pages. However, in this case, I get a JS runtime error: Error: Expected '/', and when I debug, I'm pretty sure the javascript inserted is:

<script type='text/javascript'>closeBox();<

      

As you can see, it didn't add / script> for some reason! I tried to leave tags myself and used:

Page.ClientScript.RegisterClientScriptBlock (typeof(Page), "closeBox", "closeBox();", true);

      

but with the same results.

I've also tried RegisterStartupScript to no avail.

Has anyone else encountered this before? Any ideas on what is causing it and how to fix it?

+2


source to share


3 answers


The error turned out to be something completely unrelated.



0


source


I'm not sure if this is exactly the same (my memory was not what it once was), but I think I have encountered a similar problem in the past. The solution was to put some line delimiters between different parts of the script, i.e.



Page.ClientScript.RegisterClientScriptBlock (typeof(Page), "closeBox", "<script type='text/javascript'>\n\rcloseBox();\n\r</script>");

      

0


source


Where is this RegisterClientScriptBlock register? Was it in the element <script runat="server">

?

If so, then the aspx parser will see </script>

inside your C # string literal as the end of the element it contains script

and will stop there. If you want to put a sequence </script>

inside a <script>

(and this is also the case with normal JavaScript), you must escape it to break the sequence. Good way:

"\x3C/script"

      

0


source







All Articles