user3584562
user3584562

Reputation: 63

How to Run JavaScript Function on page load

I have a program(c#) but the programs needs to work for every timezone, so i created a js script to know the client timezone and this is what i get (and it works) but only if only click action(example button), but i need this to work (run) on my page load is that possible?

This is my script

<script type="text/javascript">
    function pageLoad() {
        myFunction();
    }
    function myFunction() {
        var localTime = new Date();
        var year = localTime.getYear();
        var month = localTime.getMonth() + 1;
        var date = localTime.getDate();
        var hours = localTime.getHours();
        var minutes = localTime.getMinutes();
        var seconds = localTime.getSeconds();
        var calculated_date = hours + ":" + minutes + ":" + seconds;

        var div = document.getElementById("demo");
        div.innerText = calculated_date;

        var client_date = document.getElementById("client_date");
        client_date.setAttribute("value", calculated_date);

    }
</script>

And this is what i tryed so far

Page.ClientScript.RegisterStartupScript(this.GetType(), "myFunction", "myFunction()", true);
            Session["Data"] = client_date.Text;
            Response.Redirect("Welcome.aspx");

Thanks anyway for reading my post, hope someone can help on this issue

Upvotes: 0

Views: 4908

Answers (5)

Steve
Steve

Reputation: 11

Well you can't store the value within a hidden field and then read result, but what you can do is store the timezone offset as a cookie and then read that on the server after the page reloads.

This article explains it perfectly http://prideparrot.com/blog/archive/2011/9/how_to_display_dates_and_times_in_clients_timezone

Upvotes: 0

David
David

Reputation: 219067

The problem isn't what you think. In all likelihood, this JavaScript *is*set to run when the page loads. And it's probably going to do exactly what you told it to do. The problem is what else you're doing. Note these three lines:

Page.ClientScript.RegisterStartupScript(this.GetType(), "myFunction", "myFunction()", true);
Session["Data"] = client_date.Text;
Response.Redirect("Welcome.aspx");

What you're doing here is:

  1. Set the JavaScript to run when the page loads.
  2. Capture the value of a TextBox from before the page loaded.
  3. Abandon the page entirely and redirect to another page.

So even if the JavaScript code would run, you're not capturing the value it sets. And even if you were capturing that value, you abandon the entire page before it even loads and redirect the user to a different page.

You need to better understand the difference between server-side code, which runs on the server in its entirety before delivering a page to the client... and client-side code, which runs after a page has loaded on the client.

If you need this information from the client, you'll need to get it from client-side code. This code will need to run in a loaded page and then send the value back to the server in some way. Either the page has to execute and then perform the redirect from client-side code, or (probably better) the page can run as normal, make an AJAX post to the server to notify you of this information (time zone), and then the server can respond to the AJAX request with any data the client-side code needs to adjust the page accordingly.

It's not really clear what you're customizing based on the time zone or when you need to know the time zone. But it is clear that you need the client-side code to execute before you can retrieve that value from the client.

Upvotes: 1

Majid Akbari
Majid Akbari

Reputation: 210

Use System.Web.ScriptManager Like this :

Page.ClientScript.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "myFunction();", true);
        Session["Data"] = client_date.Text;
        Response.Redirect("Welcome.aspx");

Upvotes: 0

Chris M
Chris M

Reputation: 4115

with javascript

if (document.readyState === "complete") { myFunction(); }

with jquery

$(document).ready(function(){ myFunction(); });

Upvotes: 1

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

Yes, it is possible. All you need is to use onload() event.

This would do it

<body onload="myFunction()">
  <!-- body elements here -->
</body>

You can execute any function that you want to execute on a page load. I used myFunction because that had to be executed when the page loads.

Upvotes: 2

Related Questions