Wednesday, March 21, 2012

Passing a string to a client side function

Just replace any single and double quotes with "\char" where "char" is the
character (e.g. \' or \"). The backslash is the escape character in
JavaScript.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Jorell" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:70A4100B-D764-4FEA-B111-A5B9AE9E6529@dotnet.itags.org.microsoft.com...
> Hi everyone,
> I have an XML that I create on the fly. I need to pass this to a
Javascript function and I do it in this way:
> Page.RegisterStartupScript("Test", "<script
language=""javascript"">printHidden('" & XML & "')</script>")
> This works however if you encase the XML in single quotes then there could
possibly a terminating ' in the XML and on the other hand if you encase the
XML in double quotes and there is a single " in the XML...this becomes the
terminator and the client side throws an error: Unterminated string constant
> The user enters the information the XML is built with and we must allow
them the ability to enter these any characters.
> What I need to do is encode the string server side before it is passed and
then unencode it on the client.
> I have attempted to use Regex.Escape on the Server then unescape() on the
client with no luck.
> Any ideas/suggestions would be great! Thank you
> JorellI agree however I was kind of looking at that as a last resort. If possible I was hoping there was functions on both sides that I had missed that would take care of this for me. Any chance this is possible? Thank you again

Jorell

0 comments:

Post a Comment