<p>I need to run a JavaScript function onLoad(), but only do it if the page loaded the first time (i.e. is not the result of a postback).</p>

<p>Basically, I need to check for IsPostBack in JavaScript.</p>

<p>Thank you.</p>

[{'answer_id': 59724, 'author': 'NerdFury', 'author_id': 6146, 'author_profile': 'https://Stackoverflow.com/users/6146', 'pm_score': 2, 'selected': False, 'text': "<p>You could put a hidden input on the page, and after the page loads, give it a value.  Then you can check that field, if it was in the post data, it's a postback, otherwise it is not.</p>\n\n<p>There were two solutions that used server side code (ASP.NET specific) posted as responses.  I think it is worth pointing out that this solution is technology agnostic since it uses client side features only, which are available in all major browsers.</p>\n"}, {'answer_id': 59725, 'author': 'ctrlShiftBryan', 'author_id': 6161, 'author_profile': 'https://Stackoverflow.com/users/6161', 'pm_score': 0, 'selected': False, 'text': '<p>You can create a hidden textbox with a value of 0. Put the onLoad() code in a if block that checks to make sure the hidden text box value is 0. if it is execute the code and set the textbox value to 1.</p>\n'}, {'answer_id': 59727, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': '<p>Here is one way (put this in Page_Load):</p>\n\n<pre><code>if (this.IsPostBack)\n{\n    Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","&lt;script type=\'text/javascript\'&gt;var isPostBack = true;&lt;/script&gt;");\n}\n</code></pre>\n\n<p>Then just check that variable in the JS.</p>\n'}, {'answer_id': 59730, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 7, 'selected': True, 'text': '<p>Server-side, write:</p>\n\n<pre><code>if(IsPostBack)\n{\n   // NOTE: the following uses an overload of RegisterClientScriptBlock() \n   // that will surround our string with the needed script tags \n   ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);\n}\n</code></pre>\n\n<p>Then, in your script which runs for the onLoad, check for the existence of that variable:</p>\n\n<pre><code>if(isPostBack) {\n   // do your thing\n}\n</code></pre>\n\n<hr>\n\n<p>You don\'t really need to set the variable otherwise, like Jonathan\'s solution. The client-side if statement will work fine because the "isPostBack" variable will be undefined, which evaluates as false in that if statement.</p>\n'}, {'answer_id': 59739, 'author': 'Ian', 'author_id': 4396, 'author_profile': 'https://Stackoverflow.com/users/4396', 'pm_score': 0, 'selected': False, 'text': '<p>Lots of options here.</p>\n\n<p>For a pure JS solution, have your page submit to itself, but with additional URL parameter (mypage.html?postback=true) - you can then get the page url with window.location.href, and parse that using a split or regex to look for your variable.</p>\n\n<p>The much easier one, assuming you sending back to some sort of scripting language to proces the page (php/perl/asp/cf et. al), is to have them echo a line of javascript in the page setting a variable:</p>\n\n<pre><code>&lt;html&gt;\n\n&lt;?php\nif ($_POST[\'myVar\']) {\n    //postback\n    echo \'&lt;script&gt;var postingBack = true;&lt;/script&gt;\';\n    //Do other processing\n} else {\n    echo \'&lt;script&gt;var postingBack = false;&lt;/script&gt;\'\n } ?&gt;\n&lt;script&gt;\nfunction myLoader() {\n     if (postingBack == false) {\n          //Do stuff\n     }\n }\n\n&lt;body onLoad="myLoader():"&gt; ...\n</code></pre>\n'}, {'answer_id': 3557484, 'author': 'Faustin', 'author_id': 429637, 'author_profile': 'https://Stackoverflow.com/users/429637', 'pm_score': 5, 'selected': False, 'text': '<p>There is an even easier way that does not involve writing anything in the code behind: Just add this line to your javascript:</p>\n\n<pre><code>if(&lt;%=(Not Page.IsPostBack).ToString().ToLower()%&gt;){//Your JavaScript goodies here}\n</code></pre>\n\n<p>or</p>\n\n<pre><code>if(&lt;%=(Page.IsPostBack).ToString().ToLower()%&gt;){//Your JavaScript goodies here}\n</code></pre>\n'}, {'answer_id': 3981656, 'author': 'md1337', 'author_id': 303468, 'author_profile': 'https://Stackoverflow.com/users/303468', 'pm_score': 3, 'selected': False, 'text': '<p>The solution didn\'t work for me, I had to adapt it:</p>\n\n<pre><code>protected void Page_Load(object sender, EventArgs e)\n{\n    string script;\n    if (IsPostBack)\n    {\n        script = "var isPostBack = true;";\n    }\n    else\n    {\n        script = "var isPostBack = false;";\n    }\n    Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true);\n}\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 4219596, 'author': 'Developer_India', 'author_id': 447461, 'author_profile': 'https://Stackoverflow.com/users/447461', 'pm_score': 2, 'selected': False, 'text': "<p>Try this, in this JS we can check if it is post back or not and accordingly do operations in the respective loops. </p>\n\n<p></p>\n\n<pre><code>    window.onload = isPostBack;\n\n    function isPostBack() {\n\n        if (!document.getElementById('clientSideIsPostBack')) {\n            return false;\n        }\n\n        if (document.getElementById('clientSideIsPostBack').value == 'Y') {\n\n            ***// DO ALL POST BACK RELATED WORK HERE***\n\n            return true;\n        }\n        else {\n\n            ***// DO ALL INITIAL LOAD RELATED WORK HERE***\n\n            return false;\n        }\n    }\n</code></pre>\n\n<p></p>\n"}, {'answer_id': 12850538, 'author': 'iuppiter', 'author_id': 1566081, 'author_profile': 'https://Stackoverflow.com/users/1566081', 'pm_score': 0, 'selected': False, 'text': '<p>Create a global variable in and apply the value</p>\n\n<pre><code>&lt;script&gt;\n       var isPostBack = &lt;%=Convert.ToString(Page.IsPostBack).ToLower()%&gt;;\n&lt;/script&gt;\n</code></pre>\n\n<p>Then you can reference it from elsewhere</p>\n'}, {'answer_id': 15159545, 'author': 'Wily AO', 'author_id': 2123781, 'author_profile': 'https://Stackoverflow.com/users/2123781', 'pm_score': 4, 'selected': False, 'text': '<p>hi try the following ...</p>\n\n<pre><code>function pageLoad (sender, args) {\n\nalert (args._isPartialLoad);\n\n}\n</code></pre>\n\n<p>the result is a Boolean</p>\n'}]
['https://Stackoverflow.com/questions/59719', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3661/']