Calling a javascript function from Page_Load in code-behind

后端 未结 8 1357
广开言路
广开言路 2021-01-25 03:49

How can I call a javascript function, that is in the aspx page, from the Page_Load method, in the code-behind?

8条回答
  •  庸人自扰
    2021-01-25 03:56

    Shide's solution works. The problem is if you want to run your piece of code only on postback, for example. The solution I found was to use a "cookie": I verify if cookie x exists, if it doesn't I run the javascript code and set the cookie. When you hit refresh, the cookie will be found so the piece of code won't run.

    function pageLoad() {
       if(localStorage.getItem("cookie") === null) {
          localStorage.setItem("cookie", true);
    
          //run code
       }
    }
    

    Any code you need to run every single time just leave it out of the if statement.

提交回复
热议问题