Win10系统之家 - 专注分享最好用的电脑系统

当前位置: Win10系统之家 >  系统教程 >  电脑教程 >  window.location打开新页面

window.location打开新页面 window.location.href 使用方法详解

更新时间:2024-01-26 09:31:24作者:xiaoliu

  在我们日常的网络浏览中,经常会遇到需要打开新页面的情况,而在这个过程中,window.location.href这个方法就显得尤为重要。它是JavaScript中用于打开一个新页面或者重新加载当前页面的方法。通过操作window.location.href,我们可以根据需要动态地跳转到不同的URL地址,实现页面之间的无缝切换。在本文中我们将详细解析window.location.href的使用方法,帮助大家更好地掌握这一技巧,提升网页操作的灵活性和效率。

window.location打开新页面 window.location.href 使用方法详解 目录1、方式1: window.location.href2、方式2: window.open3、方式3 window.showModalDialog (部分浏览器不支持)4、Window 其他参考1、方式1: window.location.href
window.location.href="https://www.jb51.net";     //在当前窗口中打开窗口

类似于HTML:

<a href="https://www.jb51.net" title="测试1">Welcome Test1</a>

2、方式2: window.open
window.open("https://www.jb51.net");      //在另外新建窗口中打开窗口

类似于HTEL:

<a href="https://www.jb51.net" title="测试2" target="_blank">Welcome Test2</a>

指定参数:

<script >  var NewUrl = 'www.baidu.com' ;        window.open(NewUrl,'newindow'.'height=600,width=900,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script > 

参数说明:

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.jb51.net'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","孟子E章")  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>

4、Window 其他参考 window.open() - 打开新窗口window.close() - 关闭当前窗口window.moveTo() -移动当前窗口window.resizeTo() -重新调整当前窗口

 到此这篇关于JavaScript Window 打开新窗口的三种方式的文章就介绍到这了,更多相关JavaScript Window 打开新窗口内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

  以上就是使用window.location打开新页面的全部步骤,如果有任何疑问,请参考以上操作步骤,希望对大家有所帮助。