Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - How to execute jquery script before page load?

I need to execute javascript before Page load in ASP.NET application.

My function returns user location, and I would like to pass value to server side and load data based on location.

The problem is javascript function executes after page load.

Here is my code:

    $(function () {
        getLocation();
    });

in getLocation function I set hidden field value

$("#<%= HfLocation.ClientID %>").val(location);

in code behind I try to get value but it's always empty

protected void Page_Load(object sender, EventArgs e)
        {
            var location = HfLocation.Value;
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I need to execute javascript before Page load in ASP.NET application

This requirements makes no sense. Remember how ASP.NET works:

  1. A user request hits the web server
  2. The web server dispatches the request to ASP.NET engine.
  3. The ASP.NET engine instantiates the page and goes through the entire page lifecycle.
  4. The page is rendered as HTML and is sent to the client
  5. The client browser builds the DOM, runs client side javascript, ...

You see that it is impossible to have step 5 execute before step 3 (in which the Page_Load event executes).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...