详解js location.href和window.open的几种用法和区别

一、location.href常见的几种形式

  1. self.location.href;//当前页面打开URL页面
  2. window.location.href;//当前页面打开URL页面
  3. this.location.href;//当前页面打开URL页面
  4. location.href;// 当前页面打开URL页面
  5. parent.location.href;//在父页面打开新页面
  6. 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()打开新页面
但是用window.location.href="" 却是在原窗口打开的.
有时浏览器会一些安全设置window.open肯定被屏蔽。例如避免弹出广告窗口。

二、location.href不同形式之间的区别

a.html:

<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:

<span>这是b.html</span><span id="span1"></span><br />
<iframe src="c.html" width="500px" height="300px"></iframe>

c.html:

<span><strong>这是c.html:<strong></span><span id="span1"></span><br />
<iframe src="d.html" width="500px" height="300px"></iframe>

d.html:

<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;
b.html里面嵌着c.html;
c.html里面嵌着d.html

在d.html里面head部分写js:

function jump()
{
//经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转
//作用一样
window.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
//location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
//self.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
//this.location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
//location.href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
}

再次运行a.html,点击那个"跳转" 按钮,运行结果贴图二如下:

对比图一和图二的变化,你会发现d.html部分已经跳转到了百度的首页,而其它地方没有发生变化。这也就解释了"本页跳转"是什么意思。

修改d.html里面的js部分为:

function jump()
{
parent.location.href='http://www.baidu.com';
}

分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中嵌套的c.html部分跳转到了百度首页,这就解释了"parent.location.href是上一层页面跳转"的意思。
再次修改d.html里面的js部分为:

function jump()
{
top.location.href='http://www.baidu.com';
}

运行a.html后,再次点击"跳转" 按钮,

你会发现,a.html已经跳转到了百度首页。

分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中跳转到了百度首页,这就解释了"top.location.href是最外层的页面跳转"的意思。

三、location.href总结

看完上面的讲解之后,在来看看下面的定义你就会非常明白了:

"top.location.href"是最外层的页面跳转
"window.location.href"、"location.href"是本页面跳转
"parent.location.href"是上一层页面跳转.

location是window对象的属性,而所有的网页下的对象都是属于window作用域链中(这是顶级作用域),所以使用时是可以省略window。而top是指向顶级窗口对象,parent是指向父级窗口对象。

四、window.location.href和window.open的区别

window.location是window对象的属性,而window.open是window对象的方法
window.location是你对当前浏览器窗口的URL地址对象的参考!
window.open是用来打开一个新窗口的函数!

window.open不一定是打开一个新窗口!!!!!!!!
只要有窗口的名称和window.open中第二个参数中的一样就会将这个窗口替换,用这个特性的话可以在iframe和frame中来代替location.href。
如<iframe name="aa"></iframe>
 <input type=button  onclick="window.open('1.htm','aa','')">和
 <input type=button
  onclick="self.frames['aa'].location.href='1.htm'">的效果一样 

在给按钮、表格、单元格、下拉列表和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/'"> 

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()是只能在一个网站中打开本网站的网页

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • top.location.href 没有权限 解决方法

    这样一样,如果有人把我的网页保存后,或者用iframe包含进去,再打开的话就会直接跳到我的文章页,以前测试成功,今天看到有个网站把我的页面给包含了,我想我的首页被iframe也没什么呀,可是点内页后,发现没有进入我的网站,查看javascript错误提示发现提示"无权访问",在本地测试后发现问题依旧,看来是不对方网站做什么设置了,而是我网站问题,一步一步排除后发现,是 top.location.href 的问题,网上都大都使用的方法跟我的一样,找了半天终于有点解决方案了: 说是跨域问题

  • window.location.href中url中数据量太大时的解决方法

    先说一下今天遇到的问题 一个导出excel的处理,在按钮按下后需要传很大一个数据到后台,刚开始的做法如下: 复制代码 代码如下: var actionUrlSetData = "****Action!exportDatas.action"+ "?now=" + new Date().getTime();window.location.href= actionUrl + "&" + data; 上面的data就是一个很长的字符串. 这样在火

  • 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别

    "window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一层页面跳转 "top.location.href"是最外层的页面跳转 举例说明: 如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写 "window.location.href"."locatio

  • JS的location.href跳出框架打开新页面的方法

    今天遇到个问题,后面在框架中,当判断登录失效后要返回登录页面,但登录页面却在框架内打开,我想让它直接跳出框架打开(这里不是打开新窗口),终于在网上找到了办法,分享给大家: echo "<script language=\"javascript\">alert('登录已失效或没有登录,请登录!');location.href='login.php';</script>"; 原内容是上边这样的,要想让它跳出框架打开登录页,需要使用下面的方法: e

  • js获取当前页的URL与window.location.href简单方法

    利用JavaScript获取当前页的URL,这个问题起来好像很复杂,如果第一次去想这个问题,很多人估计又在琢磨到底又是哪个神一般的Javascript函数. 其实不是,Javascript获取当前页的URL的函数就是我们经常用来重定向的window.location.href. 比如如下函数: <script> var url=window.location.href; var loc = url.substring(url.lastIndexOf('/')+1, url.length); a

  • javascript 中设置window.location.href跳转无效问题解决办法

    javascript 中设置window.location.href跳转无效问题解决办法 问题情况 JS中设置window.location.href跳转无效 代码如下: <script type="text/javascript"> function checkUser() { if(2!=1){ window.location.href="login.jsp" rel="external nofollow" rel="e

  • location.href语句与火狐不兼容的问题

    使 用了一个点击按钮跳转的js,语句很简单: <input type="button" value="添加" onclick="location.href('http://www.jb51.net);" /> 测试的时候发现其在IE下是可以用的而firefox则不能使用了.于是我在location前面加了一个 window,即改为: onclick="window.location.href('http://baidu.co

  • window.navigate 与 window.location.href 的使用区别介绍

    首先说明的是 window.navigate 与 window.location.href 都是实现页面链接跳转的,下面将介绍它们的区别. window.navigate("http://jb51.net/") 这个方法是只针对IE的,不适用于火狐等其他浏览器,在HTML DOM Window Object中,根本没有列出window.navigate这个方法,所以这个方法尽量少用,遗忘最好. location 属性是兼容所有浏览器的.因此在实现页面跳转的时候还是使用这个比较靠谱,比如

  • location.href用法总结(最主要的)

    javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href="/url" 当前页面打开URL页面 windows.location.href="/url" 当前页面打开URL页面,前面三个用法相同. this.location.href="/url" 当前页面打开URL页面 parent.location.h

  • js获取location.href的参数实例代码

    window.location.search.substr(1); //substr(1) 是去掉参数里最开头的?号. 复制代码 代码如下: function getQuery(para){ var reg = new RegExp("(^|&)"+para +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null){ return u

  • js实现网页防止被iframe框架嵌套及几种location.href的区别

    首先我们了解一下:window.location.href.location.href.self.location.href.parent.location.href.top.location.href他们的区别与联系,简单的说:几种location.href的区别 js实现网页被iframe框架功能 "window.location.href"."location.href"."self.location.href"是本页面跳转 "p

  • JS获取url链接字符串 location.href

    js获取url链接字符串:location.href 可以对其进行截取,从而获取传送的参数,常用如下: location.href.indexOf("?")------获取?的index值. 注意:这里的location.href可不是指的现在地址栏里的地址,而是页面实际的地址. 另外,一些题外话: C#中获取字符所在位置的索引,也是用IndexOf来获取. sqlserver中获取索引,就不同了: 第一种: select * from dbo.users where CharInde

随机推荐