admin管理员组文章数量:1173665
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?
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?
4 Answers
Reset to default 32Edit
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!
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>
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>
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>
本文标签: javascriptwindowopen to center of screenStack Overflow
版权声明:本文标题:javascript - window.open to center of screen - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737625164a1999346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
onclick
? That's going to get very unreadable, very fast. – FishBasketGordo Commented Aug 8, 2011 at 14:30