0

I have a user control where I was getting the server time and doing using a AJAX timer to update it every second. This was very slow and I've been told it is better to do it on the client side.

So I came up with this...but it isn't working. Any suggestions?

    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="DateTime.ascx.vb" Inherits="UserControls_WebUserControl" %>

<script type="text/javascript">
    window.onload = function () {
        getDate();
    };

    function getDate() {
        var dt = new Date();
        var element = document.getElementById("client_time");

        element.text = dt.toDateString();
    }


</script>
   <form name="headerForm">
   <asp:Table Width="100%" ID="formatTable" runat="server">
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2">
                    <asp:Image ImageUrl="~/Logo.jpg" ID="Image1" runat="server" />
                </asp:TableCell>
                <asp:TableCell>
                    <input type="text" size="10" maxlength="10" value="" name="client_time"  />

                </asp:TableCell>

            </asp:TableRow>
        </asp:Table>
    </form>
4
  • 1
    You have filled out the name field of the element, but it lacks the id needed to find it via getElementById. Also, .text won't do anything, you need to set its value attribute. Commented Jul 30, 2012 at 16:57
  • I made those changes and the date is showing up! The text box isn't wide enough to show all of it though. Is storing it in a text box the best idea? I don't want to see the graphic of a text box, it would be nice to mesh into the background. Also, any suggestions on adding the time? Thanks! Commented Jul 30, 2012 at 17:03
  • 1
    You could put it inside of whatever element you wish to, even the table cell itself would work (with innerHTML). As for time: quackit.com/javascript/tutorial/javascript_date_and_time.cfm Commented Jul 30, 2012 at 17:06
  • Thank worked great! If you want to submit this as an Answer, I will confirm it. Thanks! Commented Jul 30, 2012 at 19:42

1 Answer 1

1

You have filled out the name field of the element, but it lacks the id needed to find it via getElementById. Also, .text won't do anything, you need to set its value attribute.

Since your input has a limit of 10 characters you might try removing that to show the full date string or you could instead simply write to the table cell with innerHTML or some such method.

To get time in a human readable format you can take a look at: http://www.quackit.com/javascript/tutorial/javascript_date_and_time.cfm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.