Pregunta

Estoy intentando abrir una ventana centrada del navegador a través de un botón de formulario usando el siguiente código...

http://www.bbc.co.uk','testwin','ancho=400, alto=400, izquierda=(screen.availWidth-400)/2, top=(screen.availHeight-400)/2' );devolver falso "> botón de prueba

El nuevo navegador aparece con la altura y el ancho correctos, pero los atributos izquierdo y superior se ignoran, por lo que la ventana aparece en la parte superior izquierda.

¿Estoy siendo demasiado ambicioso al intentar hacer que este código esté en línea?¿Hay alguna otra sintaxis que deba usar o debería rendirme y llamar a una función (preferiría no hacerlo si puedo evitarlo)?

Estoy usando Firefox 3.0.17 (más reciente), pero ocurre el mismo efecto en IE7.

Tia, Alan Harris-Reid

¿Fue útil?

Solución

http://www.tek-tips.com/faqs.cfm?fid=2296 explica cómo hacerlo.Pero yo le recomiendo el uso de un Div flotante lugar, porque de los bloqueadores de pop-up.

Digamos que usted desea abrir una ventana que tiene un tamaño de 200h x 150w.Tomando la mitad de la altura y la anchura (de 100 y 75, respectivamente) usted puede localizar el centro de la ventana.Entonces, con un poco de matemáticas puede el centro de la ventana.Aquí es cómo se hace

<SCRIPT LANGUAGE="JavaScript">
function MyPopUpWin() {
var iMyWidth;
var iMyHeight;
//half the screen width minus half the new window width (plus 5 pixel borders).
iMyWidth = (window.screen.width/2) - (75 + 10);
//half the screen height minus half the new window height (plus title and status bars).
iMyHeight = (window.screen.height/2) - (100 + 50);
//Open the window.
var win2 = window.open("filename.htm","Window2","status=no,height=200,width=150,resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
win2.focus();
}
</SCRIPT>

Para llamar a esta función en HTML, utilice esto:

<A HREF="javascript:MyPopUpWin()">This is the link</a>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top