JS newbie: Writing an ASP variable with JS

C

Calan

How can I write an ASP variable (using document.write) with JS and a CSS
class? Is there an easy way to "share" variables between client-side JS and
server-side ASP?

For an example, suppose I'm using JS to check for the browser version. If it
is > 4, I want to write a line of text (a shadow, set with CSS)containing a
server variable. Here is my pseudo-code so far, using single quotes, but it
doesn't work. The line in question is the "var" line:

<div id="PageTitle">
<h2 class="TitleForeground"><%=Session("CurrentPageTitle")%></h2>

<script language="JavaScript" type="text/JavaScript">
<!--
var titleshadow= "<h2
class='TitleForeground'><%=Session('CurrentPageTitle')%></h2>"
if
((navigator.appName=="Netscape")&&(parseInt(navigator.appVersion)>4)) {
document.write (titleshadow);
}

if
((navigator.appName=="Microsoft")&&(parseInt(navigator.appVersion)>4)) {
document.write (titleshadow);
}
//-->
</script>

</div>

In a similar way (on a different page), I need to display an image link for
higher browsers, and a straight <a> link for 4 and lower.

TIA for any help!

Calan
 
D

David Dorward

Calan said:
How can I write an ASP variable (using document.write) with JS and a CSS
class? Is there an easy way to "share" variables between client-side JS
and server-side ASP?

The direction of communication is from the server (Where ASP VBScript runs)
and the client (Where client side JavaScript runs). To send information the
other way, you have to make a new http request.
For an example, suppose I'm using JS to check for the browser version.

Which is not possibly to do reliably, and all the information you use in the
JS I snipped is also available to scripts running on the server in the ASP
environment anyway.

But why do you want to do this? There is rarely any reason to serve
different HTML content to different browsers.
 
K

kaeli

How can I write an ASP variable (using document.write) with JS and a CSS
class? Is there an easy way to "share" variables between client-side JS and
server-side ASP?

Not exactly. What you are doing is not sharing. It is dynamically
generated client-side script. The ASP generates the JS.
The problem is, ASP has no idea what the client has, and there is no way
to share client-side vars with ASP, since the communication between the
client and the server has completed once ASP hands off the page to the
client.
So, what you're really doing is making a client-side var on the fly.

var myVar = "<%= someASPVar %>";

The part in between <%= and %> is parsed on the server. Once the client
gets it, the client sees something like

var myVar = "hi there";

Now, as to your browser detection - BAD IDEA. :)
What you really want is object detection.
if (document.layers && !document.getElementById)
will be true for NN4
if (document.getElementById)
will be true for most modern browsers, including NN6+ and IE5+

Now, as to "it doesn't work" - what doesn't work? Do you bring your car
to a mechanic and tell them it doesn't work? Or do you say what happened
and what you expected to happen? :)
So - WHAT doesn't work?
You ARE using sessions, right? Since you're asking for a session
variable in the following statement.
<%=Session('CurrentPageTitle')%>
Sessions must be on and set in IIS and an ASP page must have created
one, IIRC. I am used to JSP for sessions, so I'm not positive on that,
but I do know if one hasn't been started, there wouldn't be any values.

Lastly, if this is a client-side scripting problem, it helps to know
whatthe client sees. Choose View Source and see what the browser
actually has instead of the ASP var.

--
 
B

Brian Kerrick Nickel

Everything looks fine here. Could you post the code after the page has
been processed by ASP?

Also, when looking for a higher browser, it is better to look for
capabilities rather than browser names:
if( document.layers != null ) // browser is NS 4
if( document.all != null ) // browser is IE 4 +
if( document.getElementById != null ) // browser is NS 6+, IE 5+, or a
// later version of Opera.
 
D

David Dorward

Brian said:
Everything looks fine here. Could you post the code after the page has
been processed by ASP?

Also, when looking for a higher browser, it is better to look for
capabilities rather than browser names:
if( document.layers != null ) // browser is NS 4
if( document.all != null ) // browser is IE 4 +
if( document.getElementById != null ) // browser is NS 6+, IE 5+, or a
// later version of Opera.

.... or Konqueror, or Safari, or etc.

Its never a good idea to try to detect browsers. Detecting features is good,
but they don't tell you what the browser is.

if (document.layers) { /* Browser supports document.layers so you can use
document.layers */ }
etc
 
C

Calan

Thanks much for the info guys... As usual, I was making this more difficult
than needed.

Rather than trying to access a server-side session variable, all I needed to
do was access the document title, which contains the session variable
(written server side). duh...

Long story short, here is what worked:

<div id="PageTitle">
<h2 class="TitleForeground"><%=Session("CurrentPageTitle")%></h2>

<script language="JavaScript" type="text/JavaScript">
<!--
var titleshadow = "<h2 class='TitleBackground'>" + document.title
+ "</h2>"
if( document.getElementById != null ) {
document.write (titleshadow);
}
//-->
</script>
</div>

As for the reason I am doing this...I have a page layout controlled entirely
by CSS, targeted at 5.x browsers and higher. In order to degrade gracefully
on older browsers, I have images turned off for 4.x and lower. Also, the
newer browsers display the page title with a shadow behind it by writing the
title twice with CSS positioning (until CSS3 and "text-shadow" is
supported). This causes the title to display as two lines of text in 4.x and
lower, so I want to disable one of those.

Thanks again!

Calan
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top