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 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?