dru5089
2011-12-15 08:46
采纳率: 0%
浏览 1.1k
已采纳

在窗口打开时增加COOKIE

All I want to do is increment a COOKIE for each opened tab. Management of the COOKIE variable will be in PHP, I've sorted that out, but I don`t really know how to test if a new window has been opened (Javascript or Jquery). Any heads up? Thank you!

图片转代码服务由CSDN问答提供 功能建议

我想要做的就是为每个打开的标签增加一个COOKIE。 COOKIE变量的管理将使用PHP,我已经对其进行了排序,但我真的不知道如何测试是否已经打开了一个新窗口(Javascript或Jquery)。 抬头? 谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongma6326 2011-12-15 09:06
    已采纳

    you cannot tell if a new window/tab has been opened from the server side. you can guess it however, by constantly sending a signal from an already open window to the webserver (e.g., ping the server from the page via ajax every X seconds).

    edit:

    if you only want to detect that a new tab has been on the client side only, there are several things you can do:

    • user clicks on a link/button, and you open a new window/tab because of that click (e.g., via a window.open() call), then you simply increment the cookie as you would normally increment cookies in javascript, and do it at the same place as your window.open() call.
    • if the user typing a url in a new tab is also considered opening a new window (e.g., your program isn't responsible for initiating the window...say, it was a middle/shift click on an anchor), then it gets more difficult to detect using javascript alone. One way is to add a field into local storage, and increment it when the page loads, and decrement it when the page is closed (listen for the unload event, like window.onbeforeunload). Then, to check how many windows is currently open, you can read that field. Unfortunately, this won't work on lesser browsers like IE(6|7|8).
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题