Who can help me? My fist Asp paga, doesnt work

W

Wen

Hi everyone,
..After my first lesson, I have to create a asp page with:
..asp tag for time
..client script that runs at the server for date
..client script that runs at client and shows a messagebox
These are my code, but it doenst work. Can someone help me?

<html>
<head>
<title>18 Nov 2004 My first ASP page</title>
</head>
<body>
<script language="javascript" runat="server">
var d = new Date()
var dd = d.getDate()
var mm = d.getMonth()
var yyyy = d.getFullYear();
var datum = dd + "-" + mm + "-" + yyyy
</script>

<script language="javascript">
alert("<% = Time %> " + " on " + datum)
</script>
</body>
</html>

Thanks at advance
Wen
 
R

Ray Costanzo [MVP]

datum is a server-side variable, so you need to write it into your HTML in
order for it to appear at the client. And you'll actually be writing the
variable's value, not the variable. The browser will not have any idea what
"datum" is, since that variable exists at the server level.

alert("<%=datum%>");

Ray at work
 
K

Kees Hoogendijk

Exactly as your words. I've got a error "datum not define". But how do I put
the variable's value to alert box?
Best Regards
Wen
 
R

Ray Costanzo [MVP]

You'd do that just as I included in my previous reply. What you're looking
to do is take the value of a server-side variable, and write it to the HTML
of the page, whether that be actually in a javascript function, or in a div,
or in the <title> tag, it's all the same.

<title><%=datum%></title>
<div>Here's the value of the variable from the server: <%=datum%></div>
<script type="text/javascript">alert('<%=datum%>');</script>

Ray at work
 
P

Paxton

Wen said:
Hi everyone,
.After my first lesson, I have to create a asp page with:
.asp tag for time
.client script that runs at the server for date
.client script that runs at client and shows a messagebox
These are my code, but it doenst work. Can someone help me?

<html>
<head>
<title>18 Nov 2004 My first ASP page</title>
</head>
<body>
<script language="javascript" runat="server">
var d = new Date()
var dd = d.getDate()
var mm = d.getMonth()
var yyyy = d.getFullYear();
var datum = dd + "-" + mm + "-" + yyyy
</script>

<script language="javascript">
alert("<% = Time %> " + " on " + datum)
</script>
</body>
</html>

Thanks at advance
Wen

What do you call the file name? Where do you save it? How are you trying
to run/view it?
 
D

Dave Anderson

Wen said:
Hi Ray,
Thank you so much!! It works.

Not quite. JScript uses 0-based months, so you may want to use d.getMonth()
+ 1.

ALSO -- A note of caution: This only works because the default server-side
language is VBScript. I know this because you are mixing <script
runat=server> with <%%> blocks, and there is an order of operations. To see
this in action, move your server-side JScript block to the very end of your
script, and you'll note that it still works:


<script language="javascript">
alert("<%=Time%> on <%=datum%>")
</script>

<script language="JScript" runat="server">
var d = new Date()
var dd = d.getDate()
var mm = d.getMonth()
var yyyy = d.getFullYear();
var datum = dd + "-" + mm + "-" + yyyy
</script>

A far better approach to mixing languages on the server is to encapsulate
those elements into functions (or Subs). Either of those can be called from
any other block, regardless of the parsing order. Example:

<script language="javascript">
alert("<%=Time%> on <%=getDate()%>")
</script>

<script language="JScript" runat="server">
function getDate() {
var d = new Date()
return d.getDate() + "-" +
d.getMonth() + "-" +
d.getFullYear()
}
</script>


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

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,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top