19

Possible Duplicate:
Javascript that executes after page load

i have application in asp.net 4.0

i have javascript to display client zone offset into text box:- `

<script type="text/javascript">
    function GetTimeZoneOffset() {
        var d = new Date()
        var gmtOffSet = -d.getTimezoneOffset();
        var gmtHours = Math.floor(gmtOffSet / 60);
        var GMTMin = Math.abs(gmtOffSet % 60);
        var dot = ".";
        var retVal = "" + gmtHours + dot + GMTMin;
        document.getElementById('<%= offSet.ClientID%>').value = retVal;
    }

</script>

`

Html MarkUp

<asp:HiddenField ID="clientDateTime" runat="server" />
<asp:HiddenField ID="offSet" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></body>

How can I call this function on page load so that I can display offset into textbox?

2
  • stackoverflow.com/questions/807878/…
    – Fred
    Commented Dec 29, 2012 at 5:45
  • 13
    Actually, I don't think this a duplicate. The referenced question is about general HTML / JavaScript interaction. This question and the relevant answer is specific to ASP.NET. I came here because of the ASP.NET issue. Commented Jun 21, 2015 at 12:46

4 Answers 4

30

Calling JavaScript function on code behind i.e. On Page_Load

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

If you have UpdatePanel there then try like this

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);
3
  • I tried this but page can not identify ClientScript it give error for that
    – MonaliJ
    Commented Dec 29, 2012 at 5:55
  • 1
    @MonaliJ: paste here what you tried ? Commented Dec 29, 2012 at 5:58
  • 3
    Perhaps this changed in recent versions of .NET but with framework 4, call something like this: ClientScriptManager scriptManager = Page.ClientScript; scriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:functionName(); ", true); Commented Jun 22, 2015 at 10:31
9
<html>
<head>
<script type="text/javascript">
function GetTimeZoneOffset() {
    var d = new Date()
    var gmtOffSet = -d.getTimezoneOffset();
    var gmtHours = Math.floor(gmtOffSet / 60);
    var GMTMin = Math.abs(gmtOffSet % 60);
    var dot = ".";
    var retVal = "" + gmtHours + dot + GMTMin;
    document.getElementById('<%= offSet.ClientID%>').value = retVal;
}

</script>
</head>
<body onload="GetTimeZoneOffset()">
    <asp:HiddenField ID="clientDateTime" runat="server" />
    <asp:HiddenField ID="offSet" runat="server" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</body>
</html>

key point to notice here is,body has an attribute onload. Just give it a function name and that function will be called on page load.


Alternatively, you can also call the function on page load event like this

<html>
<head>
<script type="text/javascript">

window.onload = load();

function load() {
    var d = new Date()
    var gmtOffSet = -d.getTimezoneOffset();
    var gmtHours = Math.floor(gmtOffSet / 60);
    var GMTMin = Math.abs(gmtOffSet % 60);
    var dot = ".";
    var retVal = "" + gmtHours + dot + GMTMin;
    document.getElementById('<%= offSet.ClientID%>').value = retVal;
}

</script>
</head>
<body >
    <asp:HiddenField ID="clientDateTime" runat="server" />
    <asp:HiddenField ID="offSet" runat="server" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></body>
</body>
</html>
3

Place this line before the closing script tag,writing from memory:

window.onload  = GetTimeZoneOffset;

i think the question is how to call the javascript function on pageload

2
  • Thanks for the edit @sachleen,I've posted the answer through my smart phone and there seems to be no code highlighting :-) Commented Dec 29, 2012 at 5:50
  • just add 4 spaces to the beginning of the line. :)
    – sachleen
    Commented Dec 29, 2012 at 5:52
0

use your code within

  <script type="text/javascript">
     window.onload = function() {
        var d = new Date()
        var gmtOffSet = -d.getTimezoneOffset();
        var gmtHours = Math.floor(gmtOffSet / 60);
        var GMTMin = Math.abs(gmtOffSet % 60);
        var dot = ".";
        var retVal = "" + gmtHours + dot + GMTMin;
        document.getElementById('<%= offSet.ClientID%>').value = retVal;
      }
  </script>
1
  • 2
    This is incorrect syntax and throws exceptions. The correct implementation would be: window.onload = function() { };
    – fix
    Commented Dec 20, 2016 at 14:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.