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).
Basically, I need to check for IsPostBack in JavaScript.
Thank you.
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).
Basically, I need to check for IsPostBack in JavaScript.
Thank you.
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.
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.
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.
Here is one way (put this in Page_Load):
if (this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>var isPostBack = true;</script>");
}
Then just check that variable in the JS.
Server-side, write:
if(IsPostBack)
{
// NOTE: the following uses an overload of RegisterClientScriptBlock()
// that will surround our string with the needed script tags
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);
}
Then, in your script which runs for the onLoad, check for the existence of that variable:
if(isPostBack) {
// do your thing
}
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.
Lots of options here.
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.
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:
<html>
<?php
if ($_POST['myVar']) {
//postback
echo '<script>var postingBack = true;</script>';
//Do other processing
} else {
echo '<script>var postingBack = false;</script>'
} ?>
<script>
function myLoader() {
if (postingBack == false) {
//Do stuff
}
}
<body onLoad="myLoader():"> ...
There is an even easier way that does not involve writing anything in the code behind: Just add this line to your javascript:
if(<% =(Not Page.isPostBack).ToString().ToLower() %>){//Your JavaScript goodies here}
or
if(<% =(Page.isPostBack).ToString().ToLower() %>){//Your JavaScript goodies here}
The solution didn't work for me, I had to adapt it:
protected void Page_Load(object sender, EventArgs e)
{
string script;
if (IsPostBack)
{
script = "var isPostBack = true;";
}
else
{
script = "var isPostBack = false;";
}
Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true);
}
Hope this helps.