Gabriel
Gabriel

Reputation: 792

Increment COOKIE on window open

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!

Upvotes: 1

Views: 226

Answers (1)

Chii
Chii

Reputation: 14756

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).

Upvotes: 1

Related Questions