Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.open to center of screen

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?

like image 598
manc Avatar asked Aug 08 '11 14:08

manc


1 Answers

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!

like image 62
Joseph Marikle Avatar answered Sep 21 '22 01:09

Joseph Marikle