JavaScript:Window 打开新窗口的几种方式 window.location.href、window.open、window.showModalDialog

3 篇文章 0 订阅

背景

最近在项目中使用到JavaScript中Window 打开新窗口;

先将JavaScript中Window 打开新窗口的几种方式(window.location.href、window.open、window.showModalDialog)进行知识小结备份;

实例说明

方式1

方式1: window.location.href

window.location.href="https://www.cnblogs.com/guorongtao/";     //在当前窗口中打开窗口

类似于HTML:

<a href="https://www.cnblogs.com/guorongtao/" title="测试1">Welcome Test1</a>

方式2

方式2: window.open

指定参数

window.open("https://www.cnblogs.com/guorongtao/");      //在另外新建窗口中打开窗口

类似于HTEL:

<a href="https://www.cnblogs.com/guorongtao/" title="测试2" target="_blank">Welcome Test2</a>

参数说明

  • NewUrl  //' 弹出窗口的地址; 
  • 'newwindow'   //弹出窗口的名字,非必须,可用空''代替; 
  • height=600 //窗口高度; 
  • width=900 //窗口宽度; 
  • top=0 //窗口距离屏幕上方的象素值; 
  • left=0 //窗口距离屏幕左侧的象素值; 
  • toolbar=no //是否显示工具栏,yes为显示; 
  • menubar,scrollbars //表示菜单栏和滚动栏。 
  • resizable=no //是否允许改变窗口大小,yes为允许; 
  • location=no //是否显示地址栏,yes为允许; 
  • status=no //是否显示状态栏内的信息,yes为允许;

方式3

方式3 window.showModalDialog (部分浏览器不支持)

var URL='https://www.cnblogs.com/guorongtao'

window.showModalDialog(URL,'','DialogLeft:170px;DialogTop:130px;DialogWidth:930px;DialogHeight:753px;status:no;help:no');
vReturnValue = window.showModelessDialog(sURL [, vArguments] [, sFeatures]) 

参数说明:

  •  sURL  //必选参数,类型:字符串。用来指定对话框要显示的文档的URL。
  •  vArguments  //可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。
  •  sFeatures  //可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。
  •  dialogHeight// 对话框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
  •  dialogWidth: //对话框宽度。
  •  dialogLeft: //距离桌面左的距离。
  •  dialogTop: //离桌面上的距离。
  •  center: {yes | no | 1 | 0 }://窗口是否居中,默认yes,但仍可以指定高度和宽度。
  •  help: {yes | no | 1 | 0 }://是否显示帮助按钮,默认yes。
  •  resizable: {yes | no | 1 | 0 } [IE5+]://是否可被改变大小。默认no。
  •  status: {yes | no | 1 | 0 } [IE5+]://是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
  •   scroll:{ yes | no | 1 | 0 | on | off }://指明对话框是否显示滚动条。默认为yes。

另外几个属性用在HTA中的,在一般的网页中一般不使用。

  •   dialogHide:{ yes | no | 1 | 0 | on | off }://在打印或者打印预览时对话框是否隐藏。默认为no。
  •   edge:{ sunken | raised }://指明对话框的边框样式。默认为raised。
  •   unadorned:{ yes | no | 1 | 0 | on | off }://默认为no。

传入参数:

要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大为4096个字符。也可以传递对象,例如:

//test1.htm
<script>
  var mxh1 = new Array("mxh","net_lover","浪潮行舟")
  var mxh2 = window.open("about:blank","window_mxh")
  // 向对话框传递数组
  window.showModalDialog("test2.htm",mxh1)
  // 向对话框传递window对象
  window.showModalDialog("test3.htm",mxh2)
</script>
 
//test2.htm
<script>
  var a = window.dialogArguments
  alert("您传递的参数为:" + a)
</script>
 
//test3.htm
<script>
  var a = window.dialogArguments
  alert("您传递的参数为window对象,名称:" + a.name)
</script>

可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如:

//test4.htm
<script>
  var a = window.showModalDialog("test5.htm")
  for(i=0;i<a.length;i++) alert(a[i])
</script>
 
//test5.htm
<script>
function sendTo()
{
  var a=new Array("a","b")
  window.returnValue = a
  window.close()
}
</script>
 
<form>
  <input value="返回" type=button onclick="sendTo()">
</form>

Window 其他参考

  • window.open() - 打开新窗口
  • window.close() - 关闭当前窗口
  • window.moveTo() -移动当前窗口
  • window.resizeTo() -重新调整当前窗口

参考

部分资料参考W3SCHOOLMDN

  • 15
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在Vue中,可以使用window.location.href来在当前窗口中打开一个新的URL,比如可以使用window.location.href = 'https://www.cnblogs.com/guorongtao/'来在当前窗口中打开博客园的个人博客页面。这与在JavaScript中使用window.location.href方式类似。在HTML中,可以使用<a href="https://www.cnblogs.com/guorongtao/">Welcome Test1</a>来创建一个链接,点击链接时会在新窗口打开指定的URL。另外,如果你想在Vue中打开一个新的窗口,并指定窗口的大小、位置等参数,可以使用window.open来实现,例如:window.open('www.baidu.com','newindow','height=600,width=900,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no')。另外,如果你想在Vue中打开一个模态对话框(Modal Dialog),可以使用window.showModalDialog来实现,例如:window.showModalDialog('https://www.cnblogs.com/guorongtao','','DialogLeft:170px;DialogTop:130px;DialogWidth:930px;DialogHeight:753px;status:no;help:no')。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Window 打开新窗口几种方式 window.location.hrefwindow.openwindow.showModalDialog](https://blog.csdn.net/qianluo111/article/details/125280622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪潮行舟

你的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值