Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop-Up Window, Center Screen

I'm using the following code to open a pop-up window in a Google Chrome extension, my one question is, how do I get the pop-up window to open in the centre of the users screen?

 <script>
  chrome.browserAction.onClicked.addListener(function() {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2); 
  chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': 440, 'height': 220, 'left': '+left+', 'top': '+top+', } , function(window) {
   });
});
  </script>

I've also tried this, which resulted in no such luck.

  <script>
  chrome.browserAction.onClicked.addListener(function() {
  chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': 440, 'height': 220, 'left': (screen.width/2)-(w/2), 'top': (screen.height/2)-(h/2), } , function(window) {
   });
});
  </script>
like image 901
itsdaniel0 Avatar asked Mar 17 '11 21:03

itsdaniel0


People also ask

How do you keep a pop up window always on top?

To make the active window always on top, press Ctrl + Spacebar (or the keyboard shortcut you assigned). Press the keyboard shortcut again to disable “always on top” for the active window.

How do you center a window?

To center an app window, you have to tap the Shift key three times, consecutively. The shortcut should not clash with any app on Windows 10. This app is especially useful if you have apps that consistently open off-screen because it has an option to automatically center new apps/windows that you open.


1 Answers

When you see var obj = {property: value} structure in JS it is an object creation. In your code you are trying to pass an object containing window properties to chrome.windows.create() function.

Correct code should be:

chrome.browserAction.onClicked.addListener(function() {
    var w = 440;
    var h = 220;
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2); 

    chrome.windows.create({'url': 'redirect.html', 'type': 'popup', 'width': w, 'height': h, 'left': left, 'top': top} , function(window) {
    });
});
like image 65
serg Avatar answered Sep 29 '22 14:09

serg