admin管理员组

文章数量:1312829

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.

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.

Share Improve this question asked Aug 22, 2013 at 10:32 Benjamin GruenbaumBenjamin Gruenbaum 277k89 gold badges519 silver badges514 bronze badges 12
  • 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 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 Commented Aug 22, 2013 at 11:08
 |  Show 7 more ments

3 Answers 3

Reset to default 3

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./",
  "My second screen window",
  winProps
);
}

openWindow();

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

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

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.

本文标签: javascriptGet Monitor CountStack Overflow