Learn12 Learn12 - 11 months ago 60
Javascript Question

How to disable a JavaScript function after page Page.IsPostBack

Currently, I have a very simple div that has a display: none; property. When the page loads, I call $("#MyDiv").show(); to make the div popup and show to the user on a delay. From there they can enter in information to the form and submit it with an asp.net button.

Once the user enters in the information and submits the form, it will post back to the same page but it will then run the JavaScript function again and it will show the div again. How would I disable this function after postback?

I know that you can use the code:

if (Page.IsPostBack){}

But I am not sure how to disable the function. This is what my function looks like to show the div, pretty simple:

function testpopup() {
setTimeout(function () { $("#MyDiv").show(); }, 1500);

Any Ideas? Thanks.

Ok I figured out a way to do it. I pretty much set an input field hidden with the value of N like so:

<input type="hidden" id="IsFormSubmitted" value="N" runat="server" />

Then I made the function evaluate and set the value of the input field to Y like so:


function Password() {
var val = document.getElementById("IsFormSubmitted");
if(val.value == "N")
setTimeout(function () { $("#MyDiv").show(); }, 1500);
val.value = "Y";

Thanks for all the direction :)


Perhaps you can also consider this approach - adding a HiddenField control in your aspx page where it marks whether the form has been submitted or not.

<asp:hiddenfield id="hfIsFormSubmitted" value="" runat="server"/>

With that, once the submit button OnClick event is triggered, you can assign a value to the hidden field, e.g. hfIsFormSubmitted.Value = "submitted";. Then examine this value in your JS code to decide whether or not the show() function needs to run.