Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if the browser window is moved with JavaScript?

This is for a demo... and i was just curious, can you detect if the window has been moved? Like if you move Firefox/Chrome/IE around your monitor? I doubt it, but I wanted to see since you can check for resize and focus/blurred windows.

like image 889
Oscar Godson Avatar asked Nov 30 '10 22:11

Oscar Godson


People also ask

Can I detect the browser with JavaScript?

Browser Detection with JavaScript. If you really must do it, detecting what browser someone is using is easy with JavaScript. JavaScript has a standard object called navigator that contains data about the browser being used.

How do you know when a tab has changed?

Detect tab changes with JavaScript To detect the tab change we use pure JavaScript without jQuery etc. Everything you need is hidden in this code. We register the blur event on the global document variable. Generally, the blur and focus events are often used in conjunction.

How do you check if your browser window is active or not?

You can use the focus and blur events on the window object to check if the window is still active like so: window. addEventListener('focus', function (event) { console. log('has focus'); }); window.

How do you check if a window is focused?

hasFocus() The hasFocus() method of the Document interface returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.


2 Answers

I can only think of this (heavy) work-around, where you check if window.screenX and window.screenY have changed every x milliseconds

var oldX = window.screenX,
    oldY = window.screenY;

var interval = setInterval(function(){
  if(oldX != window.screenX || oldY != window.screenY){
    console.log('moved!');
  } else {
    console.log('not moved!');
  }

  oldX = window.screenX;
  oldY = window.screenY;
}, 500);

Though I would not recommend this -- it might be slow and I'm not sure if screenX and screenY are supported by all browsers

like image 56
Harmen Avatar answered Oct 11 '22 15:10

Harmen


A potentially more optimised version of this is to only check for window movement when outside of the window combined with Harmen's answer:

var interval;
window.addEventListener("mouseout", function(evt){ 
  if (evt.toElement === null && evt.relatedTarget === null) {
    //if outside the window...
    if (console) console.log("out");
    interval = setInterval(function () {
      //do something with evt.screenX/evt.screenY
    }, 250);
  } else {
    //if inside the window...
    if (console) console.log("in");
    clearInterval(interval);
  }
});

If using jQuery, it may normalise screenX/Y in this case so it's worth running a few tests on that. Jquery would use this format instead of addEventListener:

$(window).on('mouseout', function () {});

If you are moving the window in Windows OS via alt + Space, and find that windows resizes are ignored, I would recommend adding an extra level of detection via keypress events.

like image 33
marksyzm Avatar answered Oct 11 '22 14:10

marksyzm