各浏览器对 window.open() 的窗口特征 sFeatures 参数支持程度存在差异

本文原文来至地址:http://www.w3help.org/zh-cn/causes/BX1053

   标准参考

   .......

   问题描述

使用 window.open 方法可以弹出一个新窗口,其中 open 方法的 sFeatures 参数选项在各浏览器中支持程度不一,这有可能导致同样的代码使各浏览器中弹出窗口形式产生巨大差异。

造成的影响

会造成不同浏览器中打开的窗口位置、尺寸出现差异;以及是否有地址栏、菜单栏、状态栏、滚动条和是否全屏等表现形式存在出入。

受影响的浏览器

所有浏览器

问题分析

open 方法属于 window 对象,他用来打开一个新浏览器窗口, 其中window 对象又隶属 BOM (Browser Object Model) 范畴。遗憾的是 BOM 还没有被标准化,它由各个浏览器厂商制定,因此会出现实差异。

时至今日,HTML5 规范草案中已经开始标准化 BOM,window 对象也在草案之中,草案中对 open 方法的形参数 window.open([ url [,target[,features[,replace]]]]) 做了简要列举与介绍。但是在讲到 features 时仅写着"该参数功能说明忽略",具体参数功能尚不可知。

在可查找到的浏览器厂商官方 developer 站点中, 仅有 mozilla MDC 以及 micorsoft MSDN 写有对 open 方法 features 参数说明的。其中,MDC 介绍非常简洁,仅说明参数格式,而 MSDN 中 采用了详细的参数格式和可选键值对说明,对于选项值以及选项的默认值本文将不再罗列,读者可以参看下方的 MSDN 文档说明。

Firefox 中 window.open 方法的详细说明请参看 MDC :window.open

IE 中 window.open 方法的详细说明请参看 MSDN :open Method

本文中将采用 MSDN 中列举的 features 参数选项作为评测标准,并构造以下测试代码:

<script>
function openW3C(sFeatures){
  window.open("http://www.w3.org/","",sFeatures,false);
}
</script>
<h2>请在各浏览器内依次点击下列按键,根据显示效果来判断 window.open 方法是否支持某项特性设定。</h2>

channelmode sFeatures: <button οnclick="openW3C('channelmode=no')">channelmode=no</button>
<button οnclick="openW3C('channelmode=no')">channelmode=yes</button><br />

directories sFeatures:<button οnclick="openW3C('directories=no')">directories=no</button>
<button οnclick="openW3C('directories=yes')">directories=yes</button><br />

fullscreen sFeatures:<button οnclick="openW3C('fullscreen=no')">fullscreen=no</button>
<button οnclick="openW3C('fullscreen=yes')">fullscreen=yes</button><br />

location sFeatures:<button οnclick="openW3C('location=no')">location=no</button>
<button οnclick="openW3C('location=yes')">location=yes</button><br />

menubar sFeatures:<button οnclick="openW3C('menubar=no')">menubar=no</button>
<button οnclick="openW3C('menubar=yes')">menubar=yes</button><br />

resizable sFeatures:<button οnclick="openW3C('resizable=no')">resizable=no</button>
<button οnclick="openW3C('resizable=yes')">resizable=yes</button><br />

scrollbars sFeatures:<button οnclick="openW3C('scrollbars=no')">scrollbars=no</button>
<button οnclick="openW3C('scrollbars=yes')">scrollbars=yes</button><br />

status sFeatures:<button οnclick="openW3C('status=no')">status=no</button>
<button οnclick="openW3C('status=yes')">status=yes</button><br />

titlebar sFeatures:<button οnclick="openW3C('titlebar=no')">titlebar=no</button>
<button οnclick="openW3C('titlebar=yes')">titlebar=yes</button><br />

toolbar sFeatures:<button οnclick="openW3C('toolbar=no')">toolbar=no</button>
<button οnclick="openW3C('toolbar=yes')">toolbar=yes</button><br />

top sFeatures:<button οnclick="openW3C('top=10')">top=10</button><br />

left sFeatures:<button οnclick="openW3C('left=10')">left=10</button><br />

width sFeatures:<button οnclick="openW3C('width=100')">width=100</button><br />

height sFeatures:<button οnclick="openW3C('height=100')">height=100</button><br />

top and left sFeatures:<button οnclick="openW3C('top=20,left=20')">top and left is 20</button><br />

width and height sFeatures:<button οnclick="openW3C('width=100,height=100')">width and height is 100</button><br />

top and left and width and height sFeatures:<button οnclick="openW3C('top=200,left=200,width=200,height=200')">top and left and width and height is 200</button>

由于各浏览器有不同的弹出窗口自动处理机制,为了不影响测试准确性,在运行测试代码之前因将所有浏览设置为可以打开弹出窗口。

各浏览器运行结果汇总:

  IE6 IE7 IE8 Firefox Safari Chrome Opera
channelmode不支持不支持不支持不支持不支持不支持不支持
directories不支持不支持不支持不支持不支持不支持不支持
fullscreen支持支持支持不支持不支持不支持不支持
location支持支持支持不支持7支持2不支持7支持8
menubar支持支持1支持1支持1支持1不支持9不支持9
resizable支持支持支持不支持10不支持10不支持10不支持10
scrollbars支持支持支持支持不支持11不支持11支持
status支持支持12支持不支持13支持不支持13不支持13
titlebar不支持不支持不支持不支持不支持不支持不支持
toolbar支持支持支持支持支持2不支持14不支持14
top支持3支持1支持3支持4支持4不支持5不支持5
left支持3支持1支持3支持4支持4不支持5不支持5
width支持支持支持支持支持不支持6支持
height支持支持支持支持支持不支持6支持
top left支持3支持4支持3支持4支持4不支持5不支持5
width height支持支持支持支持支持4支持1支持
top left width height支持支持4支持3支持4支持4支持3支持4

上表中为各个浏览器对 features 各参数选项的支持程度,其中需要特殊说明的如下:

  • 【标注1】:IE7 IE8 Firefox Chrome Safari 中,当 "menubar" 选项为 "yes" 时,默认不显示菜单栏,需要按 ALT 键后菜单栏才可显示;相反当 "menubar" 选项为 "no" 时,即使按了 ALT 键也不会显示菜单栏。
  • 【标注2】:Safari 中,开启 "location" 选项与开启 "toolbar" 选项时显示效果一致。
  • 【标注3】:IE6 IE8 Chrome 中,使用 "top" 和 "left" 定位,如果出现设定的的坐标值过大,弹出窗口将可能显示在屏幕可视范围外。
  • 【标注4】:IE7 Firefox Safari Opera 中,使用 "top" 和 "left" 定位,如果出现设定的的坐标值过大,窗口会自动调整 "top" 与 "left" 值,确保窗口正常显示在屏幕可视区域内。
  • 【标注5】:Chrome Opera 中,不支持在没有设定 "width" 与 "height" 值的情况下独立使用 "left" 和 "top",此时 "left" "top" 设定值均不生效。
  • 【标注6】:Chrome 中,不支持在没有设定 "left" 和 "height" 值的情况下独立使用 "width" 与 "height",此时 "width" "height" 设定值均不生效。结合【标注5】说明可知,在 Chrome 中弹出窗口不论想要设定宽高或位置中的一个或几个值,都必须将他们全部赋值,否则都将不起作用。
  • 【标注7】:Firefox Chrome 中,地址栏会始终显示。
  • 【标注8】:Opera 中,地址栏默认不显示,但可以点击页面最上方横条使他显示出来,设置 "location=yes" 后地址栏会自动显示出来。
  • 【标注9】:Chrome Opera 中,不论 "menubar" 值如何设置,永远不显示菜单栏。
  • 【标注10】:Firefox Safari Chrome Opera 中,无论 "resizable"值如何设置,窗口永远可由用户调整大小。
  • 【标注11】:Safari Chrome 中,在页面存在滚动条的情况下,无论 "scrollbars"值如何设置,滚动条始终可见。
  • 【标注12】:IE7 在 Windows XP SP3 系统中默认可以支持 "status " 参数隐藏状态栏;而在 Windows Vista 系统默认环境下不支持 "status " 参数,状态栏始终可见。这与两个系统中默认的 IE7 小版本号不同有关,前者版本号较低,后者版本号较高。
  • 【标注13】:Firefox 中,无论 "status" 值如何设置,状态栏始终可见,而 Chrome Opera 中,则与前者相反,状态栏始终不可见。
  • 【标注14】: Chrome Opera 中,无论 "toolbar" 值如何设置,始终不显示工具栏。

综上所述,可见 window.open 方法的 sFeatures 参数支持程度存在巨大差异,使用时须谨慎为之。

解决方案

建议在使用 window.open 方法的 sFeatures 参数时,推荐如下配置字符串,可确保所有浏览器表现基本一致:

" top=nInt,left=nInt,width=nInt,height=nInt,location=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no "




  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出JS中的showModal有两种不同的实现方式。 1. uniapp中自定义showModal样式 在uniapp中,可以通过自定义showModal样式来实现弹窗效果。具体实现方法如下: 1)在main.js中引入show-modal组件 ```javascript import showModal from '@/components/show-modal/show-modal.vue' Vue.component('show-modal', showModal) ``` 2)在页面中使用show-modal组件 ```html <show-modal :is-show="isShowModal" :title="modalTitle" :content="modalContent" @confirm="confirmModal" @cancel="cancelModal"></show-modal> ``` 其中,isShowModal为控制弹窗显示的变量,modalTitle为弹窗标题,modalContent为弹窗内容,confirmModal和cancelModal为点击确认和取消按钮时触发的事件。 3)在show-modal.vue中定义弹窗样式和事件 ```html <template> <div class="modal" v-show="isShow"> <div class="modal-mask"></div> <div class="modal-container"> <div class="modal-header">{{ title }}</div> <div class="modal-body">{{ content }}</div> <div class="modal-footer"> <button class="modal-confirm" @click="confirm">确认</button> <button class="modal-cancel" @click="cancel">取消</button> </div> </div> </div> </template> <script> export default { props: { isShow: { type: Boolean, default: false }, title: { type: String, default: '' }, content: { type: String, default: '' } }, methods: { confirm() { this.$emit('confirm') }, cancel() { this.$emit('cancel') } } } </script> <style> .modal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 9999; } .modal-mask { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); } .modal-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80%; max-width: 400px; background-color: #fff; border-radius: 5px; overflow: hidden; } .modal-header { padding: 10px; font-size: 16px; font-weight: bold; text-align: center; background-color: #f5f5f5; } .modal-body { padding: 20px; font-size: 14px; line-height: 1.5; text-align: center; } .modal-footer { display: flex; justify-content: center; align-items: center; padding: 10px; } .modal-confirm, .modal-cancel { padding: 5px 10px; font-size: 14px; border-radius: 5px; cursor: pointer; } .modal-confirm { margin-right: 10px; background-color: #409eff; color: #fff; } .modal-cancel { background-color: #fff; color: #333; border: 1px solid #ccc; } </style> ``` 2. window.showModalDialog()和window.showModelessDialog() 另一种实现方式是使用window对象的showModalDialog()和showModelessDialog()方法。这两个方法可以在IE浏览器中使用,但在其他浏览器中不被支持。 showModalDialog()方法可以打开一个模态对话框,即用户必须关闭对话框后才能继续操作页面。showModelessDialog()方法可以打开一个非模态对话框,即用户可以在对话框和页面之间自由切换。 这两个方法的使用方法如下: ```javascript vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures]) ``` 其中,sURL为要打开的对话框的URL地址,vArguments为传递给对话框的参数,sFeatures为对话框的特性,例如大小、位置、工具栏等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值