By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,248 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,248 IT Pros & Developers. It's quick & easy.

need to run code once rather than every time

100+
P: 121
Hi,
i am using window.onload in HTML code which is like
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2.  
  3.     window.onload = function(){
  4.       ShowPanel1(); 
  5.       return false;
  6.     }
  7.     </script>
where ShowPanel() is a function, now i have one dropdown in my form and their is a selectindexchanged postback on it, so every time dropdown is changed , this above code runs. Is their any way that this code runs only once when page initially loads. something like postback check in javascript??

Any help will be appreciated.

Regards
Jay
Jul 22 '10 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
I am not aware, that JavaScript can differenciate between initial load and reload, due to the stateless nature of HTTP.

however you can use several tricks to tell the initial script, whether it should execute or not.
- you can set a cookie, which you check before loading
- you can make the server print or omit the JS code (e.g. depending on a session)
- you can pass an URL parameter to suppress execution of the initial function
- etc.
Jul 22 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.