Y.E.P
Y.E.P

Reputation: 1207

date does not get printed : why is this so?

In the javascript function jsp I am trying to print the date.But it doesn't get printed. Why is this so ? The date should get printed before the text in the h1 tag. But the problem is date doesn't get printed ! Why is this so ?

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP - 1</title>
    <script>
        function jsp() {
        <%= new java.util.GregorianCalendar().getTime().toString() %>
        }
    </script>
</head>

<body>
    <h1>
        Was I printed first ? Or is it the date... ..
    </h1>

    <script type="text/javascript"> 
        setTimeout(jsp,2000);
    </script>
</body>

Upvotes: 1

Views: 64

Answers (1)

sp00m
sp00m

Reputation: 48807

<script>
    function jsp() {
        document.write('<%= new java.util.GregorianCalendar().getTime().toString() %>');
        // or any other JS function you may want to use
    }
</script>

You're mixing server-side and client-side.

With your original function, your browser will see (for example)

<script>
    function jsp() {
        2012-08-24 11:57:00
    }
</script>

but this isn't JS-valid (as you see).

And to answer your hidden question, the date will be printed last, because it's located after the h1 (in a DOM-speaking way).

Upvotes: 4

Related Questions