使用JavaScript获取客户端时间时出现问题. [英] Problem getting clientside time by using javascript.

查看:116
本文介绍了使用JavaScript获取客户端时间时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friendz,

我只是遇到一个小问题.我想检索用户的客户端时间.我已经为它编写了以下代码.但是有些代码没有显示时间.

Hello Friendz,

I am just stuck up with a small problem. I want to retrieve the client side time of the user. I have written following code for it.But some how its not showing the time.

function DisplayDateTime()
    {
        var DateTime = new Date();
        var strYear= DateTime.getYear();
        var strMonth= DateTime.getMonth() +1;
        var strDay = DateTime.getDate();
        var strHours = DateTime.getHours();
        var strMinutes = DateTime.getMinutes();
        var strSeconds = DateTime.getSeconds();
        var dateTime=document.getElementById("lblDateTime");
        dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
    }



我已经在单击按钮时调用了该函数.



I have called the function on the click of the button.

<asp:Button ID="btnHitMe" runat="server" Text="Hit Me!!" OnClientClick="DisplayDateTime()"OnClick="btnHitMe_Click" /

>

有人可以告诉我我要去哪里错吗?:confused:

[移出答案]:
代码:

>

Can somebody tell me where am I going wrong??:confused:

[moved from answer]:
Code:

<%@ Page Language="C#" MasterPageFile="~/masterpage1.master" AutoEventWireup="true"

    CodeFile="pgLogin.aspx.cs" Inherits="pgLogin" Title="Login Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="middleContent" runat="Server">
    <script type="text/javascript" src="script/md5-paj.js"></script>
    <script language="javascript" type="text/javascript">
    function EncryptMD5()
    {
         var Pass = document.getElementById("<%=txtPassword.ClientID%>").value;
            // Encrypte the password
             if (Pass.length > 0 )
             {
                var MD5Hash = hex_md5(Pass);
                var salt = document.getElementById("<%=SaltStr.ClientID%>").value;
                MD5Hash = hex_md5(salt+MD5Hash);
                document.getElementById("<%=HashPass.ClientID%>").value = MD5Hash;
                document.getElementById("<%=txtPassword.ClientID%>").value = MD5Hash;
             }
    }
    function DisplayDateTime()
    {
        var DateTime = new Date();
        var strYear= DateTime.getYear();
        var strMonth= DateTime.getMonth() +1;
        var strDay = DateTime.getDate();
        var strHours = DateTime.getHours();
        var strMinutes = DateTime.getMinutes();
        var strSeconds = DateTime.getSeconds();
        var dateTime=document.getElementById("lblDateTime");
        dateTime.innerHTML = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
    }
    </script>
    <table border="0" cellpadding="0" cellspacing="0" class="tblalign" style="width: 100%;
        height: 300px" title="LMS Login Page">
        <tr>
            <td align="center" colspan="5" rowspan="1">
                <hr style="width: 100%; color: #0099ff; height: 2px" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4" rowspan="1" style="height: 13px; background-color: #dedede">
            </td>
            <td align="center" colspan="4" rowspan="1" style="height: 13px; background-color: #dedede">
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4" valign="top">
                <table id="tblLogin" bgcolor="#f3f3f3" border="0" cellpadding="0" cellspacing="0"

                    style="border-right: gray 2px solid; border-top: gray 2px solid; border-left: gray 2px solid;
                    width: 300px; border-bottom: gray 2px solid" title="LMS Login Page">
                    <tbody>
                        <tr>
                            <td align="middle" class="Panelmedblack" colspan="1" rowspan="5">
                                <img src="images/Transport%20Vehical%201.jpg" />
                            </td>
                            <td align="middle" class="Panelmedblack" colspan="4" style="height: 20px">
                                Login</td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="4" height="5">
                                <asp:Label ID="lblDateTime" runat="server"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" style="height: 20px" width="2%">
                            </td>
                            <td align="center" class="Arial11" style="font-size: 9pt; font-family: Arial; height: 20px"

                                width="35%">
                                <strong><span style="color: #003399">Username:</span></strong></td>
                            <td align="left" class="medblack" style="width: 170px; height: 20px">
                                <asp:TextBox ID="txtLoginName" runat="server" CssClass="TextBoxClass" TabIndex="1"

                                    ToolTip="Enter user name" Width="145px">
                                </asp:TextBox></td>
                            <td class="medblack" style="height: 20px" width="2%">
                                <asp:RequiredFieldValidator ID="rfvLoginName" runat="server" ControlToValidate="txtLoginName"

                                    Font-Bold="True" ValidationGroup="vg" Width="120px">Enter user name</asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="4" height="5">
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" style="height: 24px">
                            </td>
                            <td class="Arial11" style="font-size: 9pt; font-family: Arial; height: 24px">
                                <div ms_positioning="FlowLayout" style="display: inline; width: 7px; height: 15px">
                                </div>
                                <strong><span style="color: #333399">Password:</span></strong></td>
                            <td align="left" class="medblack" style="width: 170px; height: 24px">
                                <asp:TextBox ID="txtPassword" runat="server" CssClass="TextBoxClass" EnableTheming="True"

                                    EnableViewState="False" TabIndex="2" TextMode="Password" ToolTip="Enter password"

                                    Width="145px">
                                </asp:TextBox></td>
                            <td class="medblack" style="height: 24px">
                                <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"

                                    ErrorMessage="RequiredFieldValidator" Font-Bold="True" ValidationGroup="vg" Width="120px">Enter password</asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td class="medblack" height="20">
                            </td>
                            <td class="medblack" height="20">
                            </td>
                            <td class="medblack">
                            </td>
                            <td class="medblack" style="width: 170px">
                                <asp:Button ID="cmdLogin" runat="server" CssClass="ButtonClass" Font-Bold="True"

                                    Font-Names="Verdana" Font-Size="Small" Height="23px" OnClick="cmdLogin_Click"

                                    OnClientClick="EncryptMD5()" TabIndex="4" Text="Login" ValidationGroup="vg" Width="53px"

                                    style="cursor: hand" ToolTip="user Login " />
                                <asp:Button ID="cmdCancel" runat="server" CssClass="ButtonClass" Font-Bold="True"

                                    Font-Names="Verdana" Font-Size="Small" Height="23px" OnClick="cmdCancel_Click"

                                    TabIndex="5" Text="Cancel" Width="61px" ToolTip="Cancel login" /></td>
                            <td class="medblack" style="text-align: left">
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="5" rowspan="2">
                                <asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" Text="Invalid Username or Password"

                                    Visible="False"></asp:Label>
                                <asp:LinkButton id="LinkButton2" runat="server" Font-Size="Small" ForeColor="Blue"

                                    PostBackUrl="~/pgChangePwd.aspx">Change Password</asp:LinkButton>
                                <hr color="gray" size="1" width="100%" />
                                <asp:HiddenField ID="SaltStr" runat="server" Visible="True" />
                                <asp:HiddenField ID="HashPass" runat="server" Visible="True" />
                                <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red" Height="17px"

                                    Width="339px"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                            </td>
                            <td>
                                <asp:Button ID="btnHitMe" runat="server" Text="Hit Me!!" OnClientClick="return DisplayDateTime()" OnClick="btnHitMe_Click" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="5" rowspan="1" style="height: 29px">
                <hr style="width: 100%; color: #0099ff; height: 2px" />
            </td>
        </tr>
    </table>
</asp:Content>




谢谢

Swapnil




Thanking You,

Swapnil

推荐答案

Ok I got your problem,

Make sure that you Have lable control on the page with ID lblDateTime ( I think this will be. )

But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting debugger; to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.

[Addition to Solution]

Ok As per you Told me that you only want to fire client Side Javascript.

Ok I got your problem,

Make sure that you Have lable control on the page with ID lblDateTime ( I think this will be. )

But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting debugger; to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.

[Addition to Solution]

Ok As per you Told me that you only want to fire client Side Javascript.

Swapnil 2009 wrote:
Swapnil 2009 wrote:

OnClientClick="DisplayDateTime()"

OnClientClick="DisplayDateTime()"



Instead Write



Instead Write

OnClientClick = "return DisplayDateTime()"





Swapnil 2009 wrote:
Swapnil 2009 wrote:

function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}

function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}




Instead Write ( Just Appended return false to the end of function )




Instead Write ( Just Appended return false to the end of function )

function DisplayDateTime() {
            var DateTime = new Date();
            var strYear = DateTime.getYear();
            var strMonth = DateTime.getMonth() + 1;
            var strDay = DateTime.getDate();
            var strHours = DateTime.getHours();
            var strMinutes = DateTime.getMinutes();
            var strSeconds = DateTime.getSeconds();
            var dateTime = document.getElementById("lblDateTime");
            dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
            return false;
       }





Vote and Mark as a Answer if it helped.





Vote and Mark as a Answer if it helped.


Can you post your ALL code of page?
Can you post your ALL code of page?


这篇关于使用JavaScript获取客户端时间时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆