Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to fix the whacky <script></script> bug in C# when dynamically creating javascript.

Reply
Thread Tools

How to fix the whacky <script></script> bug in C# when dynamically creating javascript.

 
 
andrew@transitionkiteboarding.com
Guest
Posts: n/a
 
      08-02-2005
So I spent ages trying to work out what the problem was with my code
when I did this and found a post which led me to the very simple
solution.

I use WebMatrix so I'm not sure if this is a major problem in VS or not
but it is bloody frustrating.

Stick the following bit of code into a page and save it.

<%@ Page Language="C#" %>
<script runat="server">
private void Page_Load( object sender, EventArgs e ) {
string scripttoadd = "<script></script>";
sometext.Text = scripttoadd;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Label id="sometext" runat="server"/>
</form>
</body>
</html>

So this is a pretty basic page and fairly often you'll want to add a
dynamically created script to the page to do something on the client
side...

If you try and execute this however you will get the error:

CS1010: Newline in constant

This stumped me for ages as I couldn't find the newline anywhere.
Removing the second > seemed to fix the problem but led to errors in
the HTML later on - especially where there were other scripts in the
page.

A chance post where someone mentioned the interpreter getting mangled
on something else and the colours all disappearing on the rest of my
code in WebMatrix made me realise that this is exactly what is going on
as you can't have nested <script> tags according to the the HTML spec -
and the IDEs and Interpreter don't look at the surrounding "" to see
whether you are really building a tag or not.

As such the solution if anyone else gets into this particular pickle is
to change this line:

string scripttoadd = "<script></script>";

to this line:

string scripttoadd = "<script></script" + ">";

and it will all work fine - no tripping up - I promise.

AndrewF

 
Reply With Quote
 
 
 
 
cockblocking@gmail.com
Guest
Posts: n/a
 
      08-31-2005
Awww nice... works like a charm! Thanks for saving me time bud.


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> So I spent ages trying to work out what the problem was with my code
> when I did this and found a post which led me to the very simple
> solution.
>
> I use WebMatrix so I'm not sure if this is a major problem in VS or not
> but it is bloody frustrating.
>
> Stick the following bit of code into a page and save it.
>
> <%@ Page Language="C#" %>
> <script runat="server">
> private void Page_Load( object sender, EventArgs e ) {
> string scripttoadd = "<script></script>";
> sometext.Text = scripttoadd;
> }
> </script>
> <html>
> <head>
> </head>
> <body>
> <form runat="server">
> <asp:Label id="sometext" runat="server"/>
> </form>
> </body>
> </html>
>
> So this is a pretty basic page and fairly often you'll want to add a
> dynamically created script to the page to do something on the client
> side...
>
> If you try and execute this however you will get the error:
>
> CS1010: Newline in constant
>
> This stumped me for ages as I couldn't find the newline anywhere.
> Removing the second > seemed to fix the problem but led to errors in
> the HTML later on - especially where there were other scripts in the
> page.
>
> A chance post where someone mentioned the interpreter getting mangled
> on something else and the colours all disappearing on the rest of my
> code in WebMatrix made me realise that this is exactly what is going on
> as you can't have nested <script> tags according to the the HTML spec -
> and the IDEs and Interpreter don't look at the surrounding "" to see
> whether you are really building a tag or not.
>
> As such the solution if anyone else gets into this particular pickle is
> to change this line:
>
> string scripttoadd = "<script></script>";
>
> to this line:
>
> string scripttoadd = "<script></script" + ">";
>
> and it will all work fine - no tripping up - I promise.
>
> AndrewF


 
Reply With Quote
 
 
 
 
replicant replicant is offline
Junior Member
Join Date: Jul 2006
Posts: 1
 
      07-02-2006
that method of faking it to accept </script> doesn't seem to work for me, i've tried without using String.Format, and with using String.Format:

lblDisplayScript.Text = String.Format("<script type='text/javascript' src='http://voap.weather.com/weather/oap/{0}?template=COLDV&par=null&unit=0&key=ca2d7a62e1c 6222d0e79894ab2bf0f4d'></script" + ">", zipCode);

Any suggestions?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FIX] Another possible fix for the missing ASP .Net tab problem Patrick Philippot ASP .Net 0 04-14-2006 11:48 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Perl Misc 21 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Python 23 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Java 22 03-21-2006 07:02 AM
Re: Possible fix for Bug 494589 - os.path.expandvars bug Steve Holden Python 1 07-02-2003 09:42 PM



Advertisments