473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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

Nov 19 '05 #1
1 1611
Awww nice... works like a charm! Thanks for saving me time bud.
an****@transitionkiteboarding.com 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


Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
1
by: GTi | last post by:
I want to crate a script that is run when the page load and when the page close. This can be done in the <body> section with onload and onunload (not sure about the correct syntax right now) ...
4
by: MPennig | last post by:
Here's my situation. I have a function defined in my document's <head> section which adds a <script> tag to a specified <div> tag. The added <script> has a src="" attribute pointing to a PHP file,...
1
by: Grzegorz ¦lusarek | last post by:
Hello everyone. I1m writing webb aplication using AJAX (prototype library: http://prototype.conio.net/ and scriptacuolous:http://script.aculo.us/). My Problem is that that I'm doing...
21
by: hemant.singh | last post by:
Hello all, I am try'g to send window.location.href to the server script who will generate dynamic javascript according to the referral name comg in as param Now bcz <script language="javascript"...
12
by: Iddo | last post by:
Hi, I am having a strange problem... I have an HTML file which has 2 script tags: 1) <script language="javascript" id="ABC" src="ABC.js" /> 2) <script id="general" language="javascript">...
44
by: rhythmace | last post by:
W3C HTML validator passes this: .... <script type="text/javascript" src="foo.js"> <script type="text/javascript"> ....script in here... </script> ....
2
by: boczek | last post by:
Hello all. I'm writing a small async webapp. in JavaScript and I'm using <script> element technique to load data. I'm usign <scriptbecause of cross domain restrictions with XmlRequest. The...
3
by: joe | last post by:
Is it OK to have multiple: <script type="text/javascript" src="funcs1.js"></script> <script type="text/javascript" src="funcs2.js"></script> <script type="text/javascript"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.