Why is Firefox 6 ignoring my height, width, top an

2019-07-11 05:18发布

问题:

Can anybody see what's wrong with my code? It behaves properly in IE but firefox 6 seems to ignore any height or width settings that I pass through to the javascript window.open call. I can't see anything obviously wrong with it but javascript isn't my first language so I may be making a noob error somewhere in this.

The purpose of this function is to open an 800x600 window centered on the screen and displayed modally in both IE and Mozilla family browsers.

<html>
<head>

<script language="javascript" type="text/javascript">
    function openWindow(pageURL,Title,w,h) 
    {
        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        if (window.showModalDialog) {
            window.showModalDialog(pageURL,Title,'dialogWidth:' + w     + 'px,dialogHeight:'+ h + 'px,dialogTop:'+ top + 'px,dialogLeft:' + left + ',resizable=no');
        } else {
            window.open(pageURL,Title,"toolbar=no, location=no, directories=no,     status=no, menubar=no, scrollbars=yes,resizable=no,modal=yes,     copyhistory=no,width=" + w + ", height=" + h + ", top=" + top + ", left=" + left)
        }
    }   

</script>
</head>
<body>
<a href="javascript:openWindow('http://www.google.com','Google',800,600);">Launch</a>
</body>
</html>

Just to clarify a bit, the function is designed to test for the presence of ShowModalDialog (presuming that only IE supported it) and fall into the proper window.open branch in everything that supports the W3C window.open command which implements the "Modal" option. The idea being that if ShowModalDialog was implemented then it would use that otherwise use the window.open with the "Modal" option.

回答1:

Semi-colons, not commas, in showModalDialog:

<html>
<head>

<script language="javascript" type="text/javascript">
    function openWindow(pageURL,Title,w,h) 
    {
        var left = (screen.width - w) / 2;
        var top = (screen.height - h) / 2;
        var options;
        if (window.showModalDialog) {
            options = 'dialogwidth:' + w     + ';dialogheight:'+ h + ';dialogtop:'+ top + ';dialogleft:' + left + ';resizable=no';
            console.log(options);
            window.showModalDialog(pageURL, Title, options);
        } else {
            options = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes,resizable=no,modal=yes, copyhistory=no, width=" + w + ", height=" + h + ", top=" + top + ", left=" + left;
            console.log("window.open options: " + options);
            window.open(pageURL, Title, options)
        }
    }   

</script>
</head>
<body>
<a href="javascript:openWindow('http://www.google.com','Google',800,600);">Launch</a>
</body>
</html>


回答2:

Is showModalDialog() a valid member of window? I can't find it in the documentation.

Edit: Just did a quick Google search. showModalDialog is not a W3C standard and is not implemented in Firefox.

Edit: I'm wrong. Firefox caved and started supporting it.