Jump to content

Conditional Logic set on onbeforeunload event


n1concepts
Go to solution Solved by requinix,

Recommended Posts

Hi,

 

I have the 'onbeforeunload' event set on the 'body' tag to set a JS function to trigger ONLY if a conditionis set - two var's equaling one another. Note: that all works just fine - no issues with the default setup.

 

However, This function should NOT trigger the event if the 'submit' button is click (therefore suppressing the 'process()' function set on 'body' tag. Reason: that means staff completed the transaction and data actually saved to the database. The 'cancel.html' required to inform them their action canceled the process and no record created as a result.

----

 

Here's what i have thus far - appreciate some insight to get this final piece to work ('process' function should NOT fire when 'submit' button click which triggers 'checkfire' function). That's my problem - not able to pass local var out of 'checkfire' function to update global var which then read by 'process' function. (Brain dead!)

=====

 

Here's the code for review:

* This is the snippet that sets the default var values and the 'checkfire' function that will be called from the 'submit' button

<script type="text/javascript">
var viewer = "fire";
var jscheck = viewer;

// if function called, then update var jscheck
function checkFire() {
    jscheck = "nofire";
    alert("jscheck value is now: " + jscheck);
    return jscheck;
}
</script>

* Here's the part that fires the 'cancel.html' page if staff member clicks away from the page without submitting the form data.

This is where we need to display the cancelation policy advising they did not complete the process by submitting info.

<script type="text/javascript">
// Process to advise member by leaving page without clicking "Submit Record" button, canceled the entire process / update

if (viewer == jscheck) {
function process() {
    window.open('http://www.domain/canceled.html', '_blank'); self.blur();
    }
} 
</script>

and finally, the Body tag showing 'process()' function and the 'submit' button showing onclick event calling 'checkfire()' func.

<body onbeforeunload="process()">

<input name="sumbit" value="Submit Record" onclick="checkFire();" />

Again, If the 'submit' button is clicked, then the 'process()' should NOT trigger at that point.

 

ISSUE: this is what's happening - process() function firing even when the form submitted (it should ONLY fire if staff leaves the page prior to submitting the form which is requirement to close out work order).

----

 

I'm not sure how to dynamically 'remove' the onbeforeunload event off the 'body' tag or pass the 'checkfire()' value of 'jscheck' out of that function to global jscheck var to cause the conditional logic in 'process()' to fail - resulting in no action.

 

Any help with this appreciated - thx!

Edited by n1concepts
Link to comment
Share on other sites

  • Solution

Or you could call process() regardless of everything and in there decide whether to show the cancellation notice.

<script type="text/javascript">
// Process to advise member by leaving page without clicking "Submit Record" button, canceled the entire process / update

function process() {
    if (viewer == jscheck) {
        window.open('http://www.domain/canceled.html', '_blank'); self.blur();
    }
} 
</script>
Because now, all you're doing is at the moment the page loads, deciding whether to define the process() function or not based on variables whose values can't possibly have been changed because the page only just now loaded.
Link to comment
Share on other sites

Hi,

 

Thanks to both of you for your quick responses - appreciated.

1st response: that's what i'm trying to do (hopefully i did it right) in setting conditional logic on process() - see it?

That will determined if the pop up should show policy or not - if submit button not clicked.

 

which lead to 2nd response: thanks (I will try the popup but the presentation / structure wasn't well liked by mgt but you right - most browsers/clients block popups so the redirect won't show anyway... :) To that, just adhering to mgt - they want the redirect.

 

So again, how can I adjust the code i have to update 'var jscheck' if 'checkfire()' triggered by the submit button.

That's my issue - that (global) variable remains as 'fire' regardless.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.