Using the && operator in generated JavaScript

N

Nathan Sokalski

I have a VB.NET function that I am using in an ASP.NET page. The code
creates a String, which contains && (the JavaScript Logical AND operator)
and is used as part of the JavaScript sent to the browser. However, ASP.NET
is converting this to & even though it is intended to be part of the
JavaScript. This is causing my JavaScript not to work correctly. How can I
prevent ASP.NET from doing this substitution? Thanks.
 
H

Herfried K. Wagner [MVP]

Nathan Sokalski said:
I have a VB.NET function that I am using in an ASP.NET page. The code
creates a String, which contains && (the JavaScript Logical AND operator)
and is used as part of the JavaScript sent to the browser. However, ASP.NET
is converting this to & even though it is intended to be part of the
JavaScript. This is causing my JavaScript not to work correctly. How can I
prevent ASP.NET from doing this substitution? Thanks.

I assume this is necessary when using JavaScript in an XHTML page. You
could try to use '<![CDATA[<Your JavaScript code>]]>' to surround the script
and then use unencoded entities in the script.
 
B

Bruce Barker

IE not being html 4.0 compliant does not support cdata's in script blocks,
so you have to embed them in javascript comments. (you should also add the
standard html comment inside the script block)

<html>
<body>
<script>
<!--
// <![CDATA[
if (true && true) document.write("hello world");
//]]
-->
</script>
</body>
</html>

-- bruce (sqlwork.com)


Herfried K. Wagner said:
Nathan Sokalski said:
I have a VB.NET function that I am using in an ASP.NET page. The code
creates a String, which contains && (the JavaScript Logical AND operator)
and is used as part of the JavaScript sent to the browser. However,
ASP.NET is converting this to &amp; even though it is intended to be part
of the JavaScript. This is causing my JavaScript not to work correctly.
How can I prevent ASP.NET from doing this substitution? Thanks.

I assume this is necessary when using JavaScript in an XHTML page. You
could try to use '<![CDATA[<Your JavaScript code>]]>' to surround the
script and then use unencoded entities in the script.
 
N

Nathan Sokalski

I am assuming that would work if I were putting the script code between
SCRIPT tags, but I am using my script in an event handler which I am adding
using the Attributes.Add() method, such as in the following:

Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
TextBox2.Attributes.Add("onKeyPress", jscode)

This means that my code must be a String (the second parameter of the Add
method). I basically just want this part of the code to be output literally,
without trying to "interpret" anything, which it seems to be doing with the
&'s (but amazingly not to the > or <). I have never really tried fooling
aroung with the PreRender event, but putting it in that event make a
difference?
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Bruce Barker said:
IE not being html 4.0 compliant does not support cdata's in script blocks,
so you have to embed them in javascript comments. (you should also add the
standard html comment inside the script block)

<html>
<body>
<script>
<!--
// <![CDATA[
if (true && true) document.write("hello world");
//]]
-->
</script>
</body>
</html>

-- bruce (sqlwork.com)


Herfried K. Wagner said:
Nathan Sokalski said:
I have a VB.NET function that I am using in an ASP.NET page. The code
creates a String, which contains && (the JavaScript Logical AND operator)
and is used as part of the JavaScript sent to the browser. However,
ASP.NET is converting this to &amp; even though it is intended to be part
of the JavaScript. This is causing my JavaScript not to work correctly.
How can I prevent ASP.NET from doing this substitution? Thanks.

I assume this is necessary when using JavaScript in an XHTML page. You
could try to use '<![CDATA[<Your JavaScript code>]]>' to surround the
script and then use unencoded entities in the script.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top