使用js的同窗必定知道js的location.href的做用是什么,可是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计不少人都不知道了。安全
目前在开发中常常要用到的几种形式有:服务器
1
2
3
4
5
6
|
self.location.href;//当前页面打开URL页面
window.location.href;//当前页面打开URL页面
this
.location.href;//当前页面打开URL页面
location.href;// 当前页面打开URL页面
parent.location.href;//在父页面打开新页面
top.location.href;//在顶层页面打开新页面
|
常常见到的大概有以上几种形式。框架
注:①若是页面中自定义了frame,那么可将parent、self、top换为自定义frame的名称,效果是在frame窗口打开url地址。函数 ②此外,window.location.href=window.location.href;和window.location.Reload();都是刷新当前页面。区别在因而否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,window.location.href=window.location.href;则是向指定的url提交数据.测试 ③用window.open()打开新页面
|
那么,这几种形式的跳转究竟有什么区别呢?this
直接讲定义,你确定不会理解透彻,下面我来贴四个html代码,用实际的例子讲解。google
a.html:
1
2
3
4
5
|
<
form
id
=
"form1"
action
=
""
>
<
div
><
strong
>这是a.html页面<
strong
>
<
iframe
src
=
"b.html"
width
=
"500px"
height
=
"300px"
></
iframe
> </
strong
></
strong
></
div
>
</
form
>
<
pre
>
|
b.html:
1
2
|
<
span
>这是b.html</
span
><
span
id
=
"span1"
></
span
><
br
/>
<
iframe
src
=
"c.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
c.html:
1
2
|
<
span
><
strong
>这是c.html:<
strong
></
span
><
span
id
=
"span1"
></
span
><
br
/>
<
iframe
src
=
"d.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
d.html:
1
2
3
|
<
span
>这是d.html:</
span
><
span
id
=
"span1"
></
span
><
br
/>
<
input
type
=
'button'
onclick
=
'jump();'
value
=
'跳转'
>
<
iframe
src
=
"d.html"
width
=
"500px"
height
=
"300px"
></
iframe
>
|
a.html,b.html,c.html,d.html经过iframe给联系到了一块儿,那么它们有什么的联系呢?
观察代码,咱们能够看出:
a.html里面嵌着b.html;
b.html里面嵌着c.html;
c.html里面嵌着d.html
运行a.html,贴图一以下:
在d.html里面head部分写js:
1
2
3
4
5
6
7
8
9
10
|
<strong>
function
jump()
{
//经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转
//做用同样
window.location.href=
"http://www.baidu.com"
;
//location.href="http://www.baidu.com";
//self.location.href="http://www.baidu.com";
//this.location.href="http://www.baidu.com";
//location.href="http://www.baidu.com";
} </strong>
|
再次运行a.html,点击那个"跳转" 按钮,运行结果贴图二以下:
对比图一和图二的变化,你会发现d.html部分已经跳转到了百度的首页,而其它地方没有发生变化。这也就解释了"本页跳转"是什么意思。
好,再来修改d.html里面的js部分为:
1
2
3
4
|
function
jump()
{
parent.location.href=
'http://www.baidu.com'
;
}
|
运行a.html后,再次点击"跳转" 按钮,运行结果贴图三以下:
对比图一和图三,你会发现a.html中嵌套的c.html部分已经跳转到了百度首页。
分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中嵌套的c.html部分跳转到了百度首页,这就解释了"parent.location.href是上一层页面跳转"的意思。
再次修改d.html里面的js部分为:
1
2
3
4
|
function
jump()
{
top.location.href=
'http://www.baidu.com'
;
}
|
运行a.html后,再次点击"跳转" 按钮,
你会发现,a.html已经跳转到了百度首页。
分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中跳转到了百度首页,这就解释了"top.location.href是最外层的页面跳转"的意思。
看完上面的讲解以后,在来看看下面的定义你就会很是明白了:
"top.location.href"是最外层的页面跳转
"window.location.href"、"location.href"是本页面跳转
"parent.location.href"是上一层页面跳转.
location是window对象的属性,而全部的网页下的对象都是属于window做用域链中(这是顶级做用域),因此使用时是能够省略window。而top是指向顶级窗口对象,parent是指向父级窗口对象。
1. window.location是window对象的属性,而window.open是window对象的方法 |
2. window.open不必定是打开一个新窗口!!!!!!!! |
3. 在给按钮、表格、单元格、下拉列表和DIV等作连接时通常都要用Javascript来完成,和作普通连接同样,可能咱们须要让连接页面在当前窗口打开,也可能须要在新窗口打开,这时咱们就可使用下面两项之一来完成: window.open 用来打开新窗口 window.location 用来替换当前页,也就是从新定位当前页 能够用如下来个实例来测试一下。 <input type="button" value="新窗口打开" onclick="window.open('http://www.google.com')"> <input type="button" value="当前页打开" onclick="window.location='http://www.google.com/'"> |
4. window.location或window.open如何指定target? 这是一个常常遇到的问题,特别是在用frame框架的时候 解决办法: window.location 改成 top.location 便可在顶部连接到指定页 或 window.open("你的网址","_top"); |
5. window.open 用来打开新窗口 window.location 用来替换当前页,也就是从新定位当前页 用户不能改变document.location(由于这是当前显示文档的位置)。 window.location自己也是一个对象。 可是,能够用window.location改变当前文档 (用其它文档取代当前文档),而document.location不是对象。 服务器重定向后有可能使document.url变更,但window.location.href指的永远是访问该网页时用的URL. 大多数状况下,document.location和location.href是相同的,可是,当存在服务器重定向时,document.location包含的是已经装载的URL,而location.href包含的则是原始请求的文档的URL. |
6. window.open()是能够在一个网站上打开另外的一个网站的地址 而window.location()是只能在一个网站中打开本网站的网页 |