D
D
Dmitry Tarasov2019-12-05 15:06:47
RSS
Dmitry Tarasov, 2019-12-05 15:06:47

How to check if the user is on an open tab?

I can not check whether the user is on a new open tab in any way?
I do so

newWindow = window.open('https://google.com', '_blank', 'status=0,menubar=0,toolbar=0,resizable=1,scrollbars=1,height=' + 750 + ',width=' + 1000 + ',left=' + (screen.width / 2 - 1000 / 2) + ',top=' + (screen.height / 2 - 750 / 2);
newWindow.onblur = function() { hasFocus = false; console.log('focus: false'); };

newWindow.onfocus = function() {
    hasFocus = true;
    console.log('focus: true');
 };

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Moskus, 2018-12-27
@Moskus

https://github.com/huginn/huginn

S
Sergey Sokolov, 2019-12-05
@fast-je

There is a Page Visibility API (in English).
From there, an example of code that pauses video playback if the tab becomes inactive:

spoiler
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange; 
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support 
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}
 
var videoElement = document.getElementById("videoElement");

// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
  if (document[hidden]) {
    videoElement.pause();
  } else {
    videoElement.play();
  }
}

// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
  console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
  // Handle page visibility change   
  document.addEventListener(visibilityChange, handleVisibilityChange, false);
    
  // When the video pauses, set the title.
  // This shows the paused
  videoElement.addEventListener("pause", function(){
    document.title = 'Paused';
  }, false);
    
  // When the video plays, set the title.
  videoElement.addEventListener("play", function(){
    document.title = 'Playing'; 
  }, false);

}

E
Eugene, 2019-12-05
@toohappy

Well, I do not know. Check trite events.
For example, on the window scroll event in the browser, if the scroll values ​​change, then the user is in a new tab, then send the data to the server if necessary. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question