14

I am using window.open to open a popup window like so:

<a href="http://path/to/url" onclick="window.open(this.href, 'sharegplus', 'height=485,width=700'); return false;" target="_blank">

I want this to be centered in the screen, but without having to use <script> inline code </script> and merely enter whatever I need within onclick="". Can this be done?

manc
  • 471
  • 3
  • 6
  • 15
  • @Rionmonster The accepted answer to that question is a pretty terrible script, and not even complete. – Pointy Aug 08 '11 at 14:26
  • @Pointy, perhaps you could improve upon it and post. Or, at least give some guidance to Alex about what makes it terrible. – Brad Aug 08 '11 at 14:27
  • Why do you want it only in the `onclick`? That's going to get very unreadable, very fast. – FishBasketGordo Aug 08 '11 at 14:30
  • unless you can make the best alternative. it must be able to be used multiple times in one page, but the width, height and position will always stay the same – manc Aug 08 '11 at 14:32
  • One of reasons of using inline event `onclick` is to pass over popup blockers. According to many SO answers only inline JS can open new window without activating popup blocker. – Grzegorz Gierlik Feb 05 '13 at 13:19
  • 2
    Possible duplicate of [Center a popup window on screen?](http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen) – AMIC MING Mar 21 '16 at 19:41
  • Check This [Window in center screen](https://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen) – Mohsin Afzal Mar 07 '19 at 06:42

4 Answers4

32

Edit

This is a bad answer. a much better answer can be found here: window.open() on a multi-monitor/dual-monitor system - where does window pop up?

But in the meantime while i decide when i want to update this answer, this fiddle accounts for dual monitor setups: http://jsfiddle.net/w665x/138/

Original Answer

This might work for you. Not confident in it being entirely cross-browser, but close;

<html>
<head>
<script type="text/javascript">
function goclicky(meh)
{
    var x = screen.width/2 - 700/2;
    var y = screen.height/2 - 450/2;
    window.open(meh.href, 'sharegplus','height=485,width=700,left='+x+',top='+y);
}
</script>
</head>
<body>
<a href="http://path/to/url" onclick="goclicky(this); return false;" target="_blank">blah</a>
</body>
</html>

Fiddle!

Community
  • 1
  • 1
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
1

Also you can try:

$('a.popup-window').on('click', function(){
    var w = 880, h = 600,
        left = Number((screen.width/2)-(w/2)), tops = Number((screen.height/2)-(h/2)),
        popupWindow = window.open(this.href, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=1, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
    popupWindow.focus(); return false;
});

And call:

<a href="https://google.com/" class="popup-window">Open in new window</a>
Paramtamtаm
  • 312
  • 5
  • 12
0

This code works perfectly:

//Code Starts
$(document).ready(function() {
   $('#Popup').click(function() {
     var NWin = window.open($(this).prop('href'), '', 'height=800,width=800');
     if (window.focus) 
     {
       NWin.focus();
     }
     return false;
    });
});​
//Code Ends


<a href="http://www.google.com/" id="Popup">Open in Popup window</a>
S. S.
  • 47
  • 2
  • 6
0

This code uses jAplus script. It allows you to do this without writing JavaScript code. (http://japlus.simplit.it)

<head>
   <script src="/path/to/jquery.js" type="text/javascript" charset="utf-8"></script>
   <script src="/path/to/jquery.Aplus.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <a href="http://path/to/url" class="win win-center">
</body>
</html>
erik
  • 63
  • 1