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

在窗口打开时增加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!

  • 写回答

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).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度