views:

245

answers:

3

I'm currently trying to register a JS function call from a .NET page which simply calls a small function on the .aspx page that will close the window.

The RegisterClientScriptBlock code is like this:

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

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

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

As you can see, it hasn't added the /script> for some reason! I've tried to leave out the 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 come across this before? Any ideas on what's causing it and on how to fix it?

A: 

I'm unsure if it's exactly the same (my memory not being what it once was) but I think I've hit a similar problem in the past. The solution was to put some line terminators between the various parts of the script, i.e.

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

Where was this RegisterClientScriptBlock? Was it in a <script runat="server"> element?

If so, the aspx parser will see the </script> inside your C# string literal as being the end of the enclosing script element and will stop there. If you want to put the sequence </script> inside a <script> (and this goes for normal JavaScript too) you must escape it to break up the sequence. A good way is:

"\x3C/script"
bobince
Good to know, thanks!But the whole thing turned out to be a false alarm, it was something else entirely (as briefly described above), embarassingly enough.
Ian Devlin
A: 

The error turned out to be something completely unrelated.

Ian Devlin