Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,
How to call a client side javascript function from server side.
Please help.
I am waiting.. ..I want to call dTime() from Server side on (Page_Load(object sender, EventArgs e).
Example:

XML
<script language="javascript" type="text/javascript">
    function dTime() {

        var d = new Date();

        var h = d.getHours();

        var m = d.getMinutes();

        var s = d.getSeconds();



        //add a zero in front of numbers < 10

        m = checkTime(m);

        s = checkTime(s);


        document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
        t = setTimeout('dTime()', 500);
    }

    function checkTime(i) {

        if (i < 10) {

            i = "0" + i;

        }
        return i;
    }

</script>


XML
<tr>
        <div id="txt">
         </div>

       <td>

         <form action="#">

        </form>

    </td>
    </tr>




Thanks,
Emon Mahmud
Posted
Updated 18-Oct-11 2:05am
v3
Comments
BobJanova 18-Oct-11 8:14am    
Just a note regarding the (correct) solutions below, and your terminology: You are not actually calling the JS method from the server. You are merely putting it in the output stream so that the browser executes it. You can't, for example, get a result from a method or see the server state from within the JS.

use this

C#
Page.RegisterStartupScript("page", "<script language='javascript'>dTime()</script>");


in place of page.RegisterStartupScript you can also use ClientScript.RegisterClientScriptBlock

if you are using update panel in your aspx page then use

ScriptManager.RegisterClientScriptBlock
for scriptmanager see this[^]
 
Share this answer
 
v4
You can use: MSDN:ClientScriptManager.RegisterClientScriptBlock Method[^]

This adds your script to the HTML sent to the client and will be executed on the client.
 
Share this answer
 
Hi,

you can try this for your requirement

HTML
<body onload="dTime()">


by using this you can activate it on pageload from client side also

All the Best
 
Share this answer
 
check below

we have 2 java scrpt functions here
1) MaskMoney
2) ValidateQAPTTHours
C#
txtQAHExpLv2To.Attributes.Add("onKeypress", "return MaskMoney(event)");
            txtMTHFTHours.Attributes.Add("onblur", "ValidateMTFTHours();");
            txtMTHPTHours.Attributes.Add("onblur", "ValidateMTPTTHours();");
            txtMTHICHours.Attributes.Add("onblur", "ValidateMTTotHours();");
            txtQAHFTHours.Attributes.Add("onblur", "ValidateQAFTHours();");
            txtQAHPTHours.Attributes.Add("onblur", "ValidateQAPTTHours();");
            txtQAHICHours.Attributes.Add("onblur", "ValidateQATotHours();");
 
Share this answer
 
Call this function in the body load itself

<body onload="dtime()"></body>
 
Share this answer
 
thnks for the answer, i used it
 
Share this answer
 

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