最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Kill sessions on browsertab close - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Dec 22, 2010 at 10:48 Carlos MoraCarlos Mora 1,1942 gold badges13 silver badges31 bronze badges 2
  • 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 Commented Dec 22, 2010 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 Commented Dec 23, 2010 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论