n1concepts Posted September 23, 2014 Share Posted September 23, 2014 (edited) 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 September 23, 2014 by n1concepts Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/ Share on other sites More sharing options...
Solution requinix Posted September 23, 2014 Solution Share Posted September 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/#findComment-1491899 Share on other sites More sharing options...
CroNiX Posted September 23, 2014 Share Posted September 23, 2014 Personally I'd use a js alert() method to display the cancellation policy, or some other javascript modal/overlay. If their browsers settings are blocking popups (most do) they will never see the cancellation policy since it's trying to open a new tab. 1 Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/#findComment-1491900 Share on other sites More sharing options...
n1concepts Posted September 23, 2014 Author Share Posted September 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/#findComment-1491906 Share on other sites More sharing options...
CroNiX Posted September 23, 2014 Share Posted September 23, 2014 See the difference between requinix's process() code and yours as far as the conditional? His does the check within the process() function. Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/#findComment-1491907 Share on other sites More sharing options...
n1concepts Posted September 23, 2014 Author Share Posted September 23, 2014 Yeah, i see it - and that will work (Thank you both)! Quote Link to comment https://forums.phpfreaks.com/topic/291240-conditional-logic-set-on-onbeforeunload-event/#findComment-1491909 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.