18

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?

Community
  • 1
  • 1
MonaliJ
  • 213
  • 2
  • 4
  • 11
  • http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load – Fred Dec 29 '12 at 05: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. – James John McGuire 'Jahmic' Jun 21 '15 at 12:46

4 Answers4

29

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);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
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>
JamesFaix
  • 8,050
  • 9
  • 37
  • 73
Bilal Fazlani
  • 6,727
  • 9
  • 44
  • 90
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

Community
  • 1
  • 1
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
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>
ChaoticCoder
  • 195
  • 5
  • 14
Manish Nagar
  • 1,038
  • 7
  • 12
  • 2
    This is incorrect syntax and throws exceptions. The correct implementation would be: window.onload = function() { }; – fix Dec 20 '16 at 14:01