简体   繁体   中英

Popup centering horizontally but not vertically

I am trying to center a popup window with javascript, but it only centers it horizontally, not vertically.

Vertically it's stuck at the top of the screen. I tried adjusting the "top" variable, but whatever I change it to, it stays at the top of the screen.

string url = "LoginPage.aspx";

StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'> var height=500/((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth); var width=550/((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth);");
sb.Append("var left = ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) - 550) / 2 / ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth) + (window.top.screenLeft !== undefined ? window.top.screenLeft : window.top.screenX);");
sb.Append("var top = ((window.top.innerHeight ? window.top.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height) - 500) / 2 / ((window.top.innerWidth ? window.top.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width) / window.top.screen.availWidth) + (window.top.screenTop !== undefined ? window.top.screenTop : window.top.screenY);");
sb.Append("window.open('");
sb.Append(url);
sb.Append("','_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no,");
sb.Append("width='+width+',height='+height+',top='+top+',left='+left);");
sb.Append("</script>");

ClientScript.RegisterStartupScript(this.GetType(), "loginscript", sb.ToString());

My solution is taken from: Center a popup window on screen?

I found the solution. The problem was the variable name "top". I don't know why, but top returned a window object instead. I changed the variable name to "topp", and that worked. I have no idea why, but if anybody knows I'm curious!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM