6

I have a web application that I'd like to implement the following behavior.

When you click on a specific link:

  • If the user has more than one monitor - open the url in a window with a given name.

  • Otherwise - change the iframe's src attribute to that of the url.

How do I detect the monitor count in JavaScript?

If it's impossible, a Chrome only solution would also work since this is for an intranet app.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I suspect you'll need to make it a config option rather than derive it. Config is probably better anyway, user's like choice, and don't like choices made for them (unless that's a requirement of the intranet app). – AD7six Aug 22 '13 at 10:41
  • @AD7six What about opening a new window, then giving it an offset with `moveTo` and checking `window.screenX` or something? – Benjamin Gruenbaum Aug 22 '13 at 10:48
  • Why do you want to spam the user with new windows and wouldn't it be better to implement this as a choice or configuration option for the user instead? – Spoike Aug 22 '13 at 11:03
  • Unfortunately I don't have a solution - for me at least (never looked at this problem before) spawned windows are contained within the current screen. – AD7six Aug 22 '13 at 11:05
  • @AD7six What about `moveTo`? I'll have to test it on Windows, behavior might be different than on awesome (or other WMs) – Benjamin Gruenbaum Aug 22 '13 at 11:08
  • @Spoike It always opens _the same_ window causing a refresh, I want to utilize two monitors only when available and yes - this is a requirement, don't worry the user gets to opt out if they want to). – Benjamin Gruenbaum Aug 22 '13 at 11:09
  • [a big fat no Benjamin](http://ad7six.com/dump/fale.png) - of course may vary depending on OS (ubuntu here) - but I doubt it. – AD7six Aug 22 '13 at 11:11
  • @AD7six I'm satisfied, you should post this as an answer. – Benjamin Gruenbaum Aug 22 '13 at 11:16
  • @BenjaminGruenbaum What do you mean with "opening a window that causes a refresh"? The requirement does not seem to make sense to me and AFAIK you can't do multiple monitor detection in JavaScript. You may need to have the requirement changed or solve the *actual* problem in a different manner. – Spoike Aug 22 '13 at 11:18
  • The actual problem is displaying a different UI depending on whether or not the user is currently using a computer with multiple monitors. By "opening a window.." I meant that if you call `window.open` with a window name of an already existing window, it will change that window and not spawn a new one. – Benjamin Gruenbaum Aug 22 '13 at 11:19
  • @BenjaminGruenbaum that might be a premature conclusion - I'm not sure hence it's not an answer. Infact personally I can only spawn popups on the first screen irrespective of which screen has the parent window (coincidentally I almost always use the left most monitor for browser - and block popups =)). – AD7six Aug 22 '13 at 11:34
  • Monitor counts seem kind of useless. What you need is desktop dimensions, monitor names and dimensions of each monitor. You also need the top left coordinates for each monitor name. – WinEunuuchs2Unix Apr 09 '22 at 00:43

3 Answers3

2

Edit: consider window.screen.isExtended and the answer below (hopefully above by the time you are reading this)

As others have said the only sensible way to do this is to ask the user. A proposal to allow this has been raised and declined before. Multi monitor setups are not very common in the general population which is why this is ignored.

The alternatives are:

  • Prompt the user.
  • Ask the user to run a browser extension (less fun and more risky).
  • Run through a plugin (also not very fun).
  • Serve your app through something like WebkitJS

Overall - for normal websites the only option is to prompt the user.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Just curious if this answer is still true 7 years later? I have three monitors, one is a TV usually with picture turned off and sound on. Inevitably lots of Browser stuff goes there because that monitor is top-left coordinates 0,0. The monitor I'm typing on is top-left coordinates 3840, 2160. My main monitor is top-left coordinates 1920,0 and it extends to bottom-right coordinates 5760, 2159. Having to key x,y,h,w numbers in will not be fun :( An alternative would be to pop-up a sample window and have user drag it where they want real window to go. Then query the x,y,h,w of the sample? – WinEunuuchs2Unix Apr 08 '22 at 23:21
  • @ WinEunuuchs2Unix, placing a window on a specific screen is now possible - please see my answer below. – user1207504 Mar 10 '23 at 10:47
1

This is not currently not possible with Javascript, you could perhaps attempt to take a guess by calculating the screen height and width.

For example if the screen width is wider than the height * 1.78 then it means that it is more than a 16:9 aspect ratio then they are probably using a dual screen.

Well then would window.innerWidth be better than screen.width?

Other than that, this is not possible.

iConnor
  • 19,997
  • 14
  • 62
  • 97
1

Greetings from the future!

In 2023, we can check with

window.screen.isExtended;

whether there is a second monitor. Then we can use

window.getScreenDetails();

to get the amount of screens, their display details (of which there are many) and their 'left' coordinates. This requires the user to grant permission, though.

With this, you can open a window on a specific screen. For example:

// If you run this on your left screen (0),
// the window will open on your right screen

async function openWindow(){
// Get screen info
const details = await getScreenDetails();
const secondScreen = details.screens[1];
const {left} = secondScreen;

// Open window
const winProps = `left=${left},top=100,width=640,height=480`;
const win = window.open(
  "https://www.google.com/",
  "My second screen window",
  winProps
);
}

openWindow();

Read more at https://developer.chrome.com/articles/multi-screen-window-placement/#the-window-management-permission

user1207504
  • 337
  • 3
  • 10