I save the position of a window when it’s closed:
const savePositionToStorage = (position) => {
localStorage.setItem('windowPosition', position);
console.log('Position saved:', position);
}
const getPositionFromStorage = () => {
const position = localStorage.getItem('windowPosition');
return position ? position : "top=0,left=0,width=800,height=1000";
}
and open a window like this:
const handleExpandClick = (id) => {
const windowFeatures = getPositionFromStorage();
const pdfWindow = window.open(
`http://127.0.0.1:8000/transactions/matching_evidence/${id}#toolbar=0,view=FitH`,
"pdf",
windowFeatures,
);
pdfWindow.onload = () => {
pdfWindow.onbeforeunload = () => {
savePositionToStorage(`top=${pdfWindow.screenY},left=${pdfWindow.screenX},height=${pdfWindow.innerHeight},width=${pdfWindow.innerWidth}`)
}
}
}
The user should be able to save the current location and size of the window, so the user doesn’t have to readjust the pop-up every time.
It works with the current browser monitor, but if I close it on another monitor, it will simply pop up on the furthest right of the current browser monitor.
Is there a way to archive what I want?
Chrome would be the browser the users are using.
2
Answers
For some reason, it works now. I deleted the local storage, and when I tried again, it just worked. I didn't really change anything, except that I'm saving the data as a stringnifyed json:
I will ask my colleagues to try the code later and I'll report back if it works there. Very strange
Edit: Okay, the problem seems to be Window management. This has to be set to allow. I don't know how to get the permissions yet
Edit: I found the problem now. I simply needed to wait for the promise and go from there (full code):
Be aware that this won't work with FireFox https://developer.mozilla.org/en-US/docs/Web/API/Window/getScreenDetails
I’ve wondered the same thing myself. Unfortunately, there’s no direct way to reliably determine the monitor a window was last closed on using JavaScript. Browsers do not provide an API to get the monitor information for a window.
Here’s why it’s tricky:
Workarounds
Here are some alternative approaches to consider:
1. Manual Adjustment:
2. (Partial) Solution using
screen.availHeight/Width
:screen.availHeight
andscreen.availWidth
to get the available screen space, which can give you an idea of the monitor’s resolution.Example (Partial Solution):
3. User Input:
4. Third-Party Libraries:
Conclusion
While a perfect solution to automatically determine the monitor is not available, these workarounds can offer a more user-friendly experience.