1

I've been trying to solve how to close session when browser/tab is closed. Following the Daniel Melo's answer + setting session's coockie TTL to 0 i'm almost ready, but Daniel's tip is not working for input type=[button].

var validNavigation = false;

function endSession() {
    document.cookie = 'MYOWNSESSID=; path=/';
}

function okEvent(){
     validNavigation = true;
  }

function wireUpEvents() {

  window.onbeforeunload = function() {
      if (!validNavigation) {
         endSession();
      }
  }

  $("a").bind("click", okEvent );

  $("form").bind("submit", okEvent );

  // that's my adds that don't work
  $(":button").bind("click", okEvent );
  $(":button").bind("submit", okEvent );
  $(":button").onclick= okEvent;
}


// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
    wireUpEvents();
});

The key if to flag an event as valid before unloading occurs. I'm trying to hook the buttons click but without success. The jquery selector is working fine, i checked that $(':button').length is > 0. Sure i'm missing something but can't find it.

Community
  • 1
  • 1
Carlos Mora
  • 1,164
  • 2
  • 12
  • 28
  • Nothing wrong in your code...actually your addition is not needed since you are calling the `okEvent` function on the submit event on the form. Anyway, can you put and alert in the okEvent to check if it's actually setting the flag? – ifaour Dec 22 '10 at 20:49
  • Thanks ifaour. what you sugest is exactly what i've done. I put an alert on okevent() so i know when it is called, that works fine except for the input type[button]. – Carlos Mora Dec 23 '10 at 15:25

1 Answers1

2

Ok, here are some tips:

-replace the three lines you've added with:

$(':button').click(okEvent);

-Don't bind the submit event on a Button input, not to be confused with the Submit input (more):

<input type="submit" value="Submit" />

-if you are using the Button to submit the form, then use the submit input instead and remove all your additions since the submit event is already attached to the form:

$("form").bind("submit", okEvent );

Please check this test to better understand the Button input.

ifaour
  • 38,035
  • 12
  • 72
  • 79
  • Thanks for your answer. I'm not submitting with the button, there are a submit input and a button to cancell the form editting. $(':button').click(okEvent); – Carlos Mora Dec 30 '10 at 10:47
  • Please see this, you will see that it looks like i need to wrap the original click, something a little bit complex for my current level, http://jsfiddle.net/carlosmora/zkG9u/ – Carlos Mora Dec 30 '10 at 11:19
  • Changing your example the problem showns: the onclick for buttons is executed before the okEvent to validate the execution, so the problem seems to be reduced to change the order and put the original event handler to be executed after. http://jsfiddle.net/carlosmora/ZnBUU/ – Carlos Mora Dec 30 '10 at 11:38
  • You shouldn't do that, instead use callback to insure that everything will be executed when it should: [example link](http://jsfiddle.net/ifaour/ZnBUU/1/) – ifaour Dec 30 '10 at 11:55
  • That's right, that's the way but i need to transparently wrap the page events, without recoding every button. My target was to validate the unload event, so the session get's killed if navigation is not made from page elements. Recoding every button is a solution but i'm looking something automagic, like anchors and submit do. – Carlos Mora Dec 30 '10 at 12:24
  • I didn't get your comment here, but shouldn't [this](http://jsfiddle.net/ifaour/zkG9u/1/) be what you are asking for? – ifaour Dec 30 '10 at 12:33
  • Thanks ifaour for your attention, you deserve at least a beer. What i want to do is to avoid having to write nothing special in the buttons, including having the validated action in the onclick. It's just to include the script to make all happen automatically. It's not necesary to write someting special in anchors nor submits, they work fine with the script. http://jsfiddle.net/carlosmora/hwBKy/ – Carlos Mora Dec 30 '10 at 12:56
  • lol, I'm totally lost now...if you don't to write anything in the markpup, then my last fiddle (same as urs) is just what you want, and the "*script*" is doing everything now, right? :-) – ifaour Dec 30 '10 at 13:06
  • excese me, my english is very bad so sometimes i don't explain very well. I'm just trying exactly the opposite: keep everything in the markup, and write a jscript that wraps any intent to navigate to make this navigation valid. That implies that button needs the onclick attrib, and keep it there, so all the code already written keeps working. Hope this time i can explain http://jsfiddle.net/carlosmora/zkG9u/2/ – Carlos Mora Dec 30 '10 at 14:27
  • Got it! I'm not sure it is the most elegant way, but makes what i'm looking for. Thanks for your support! http://jsfiddle.net/carlosmora/hwBKy/2/ – Carlos Mora Dec 30 '10 at 14:55
  • hmm, I see. But still can't understand why you are doing this, it's not like a `href`..it's still a JS call. Anyway glad you got it working! – ifaour Dec 30 '10 at 15:17
  • The meaning of all that is that if the script is supressed everything still works (without the unload checking, of course). In this way it is enough to include the script to activate the unload event control, and kill the session in case of closing the tab or browser. – Carlos Mora Dec 30 '10 at 15:35