Click here to Skip to main content
15,918,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends,

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

JavaScript
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;
    }


Now I want to call this function on the page load event.I cannot call this on load of body because my page in engulfed in the master page. So probably I will have to call it from code behind.

So can anyone tell me how to accomplish this?

Ok friendz I have found something here. If thepage is engulfed in a master page then the java script code doesn't work. But if I call it from a simple page without master page it works fine.

Can anyone tell me about this problem?

Thanking You

Swapnil Borkar
Posted
Updated 10-Oct-10 23:12pm
v6

Well, try something like:
C#
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "DisplayDateTime()", true);


P.S.: The RegisterClientScriptBlock method adds a script block to the top of the rendered page. While, the script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised.
(it means the RegisterStartupScript method adds a script block to the end of the rendered page.)
 
Share this answer
 
Comments
Swapnil 2009 11-Oct-10 3:36am    
Hey Sandeep, I tried that solution of yours but that doesn't seem to work in my case. The client side time is not displayed on page load. Can you help to sort out the problem?
Ok friendz I got it.

Its because I was using

var dateTime=document.getElementById("lblDateTime");

in my code.

Since I accessing the field on master page I will have to write

var dateTime=document.getElementById("ctl00_ContentPlaceHolder1_lblDateTime");

but there is one more problem.

This solution does not work on Windows 7...:confused:

Problems!!! Problems!! :((
 
Share this answer
 
Comments
Dalek Dave 11-Oct-10 5:43am    
There are always new things to screw up :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900