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


Posted in Javascript onDecember 02, 2019

一、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>

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

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,点击那个"跳转" 按钮,运行结果贴图二如下:

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

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

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

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

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

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

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

Javascript 相关文章推荐
arguments对象
Nov 20 Javascript
Javascript 获取链接(url)参数的方法[正则与截取字符串]
Feb 09 Javascript
javascript实现颜色渐变的方法
Oct 30 Javascript
js去除输入框中所有的空格和禁止输入空格的方法
Jun 09 Javascript
Javascript中数组sort和reverse用法分析
Dec 30 Javascript
JavaScript插件化开发教程 (四)
Jan 27 Javascript
浅谈js的url解析函数封装
Jun 28 Javascript
Angularjs中三种数据的绑定策略(“@”,“=”,“&amp;”)
Dec 23 Javascript
微信小程序数字滚动插件使用详解
Feb 02 Javascript
一次微信小程序内地图的使用实战记录
Sep 09 Javascript
使用layui的layer组件做弹出层的例子
Sep 27 Javascript
node.js中module模块的功能理解与用法实例分析
Feb 14 Javascript
js实现一款简单踩白块小游戏(曾经很火)
Dec 02 #Javascript
JavaScript 自定义html元素鼠标右键菜单功能
Dec 02 #Javascript
VUE 动态组件的应用案例分析
Dec 02 #Javascript
VUE 直接通过JS 修改html对象的值导致没有更新到数据中解决方法分析
Dec 02 #Javascript
vue 动态表单开发方法案例详解
Dec 02 #Javascript
vue 开发之路由配置方法详解
Dec 02 #Javascript
vue 开发企业微信整合案例分析
Dec 02 #Javascript
You might like
php数组的一些常见操作汇总
2011/07/17 PHP
浅谈Eclipse PDT调试PHP程序
2014/06/09 PHP
jquery的冒泡事件的阻止与允许(三种实现方法)
2013/02/01 Javascript
用JS中split方法实现彩色文字背景效果实例
2016/08/24 Javascript
angular.js之路由的选择方法
2016/09/24 Javascript
jQuery中的AjaxSubmit使用讲解
2016/09/25 Javascript
canvas实现手机端用来上传用户头像的代码
2016/10/20 Javascript
JS简单封装的图片无缝滚动效果示例【测试可用】
2017/03/22 Javascript
MUI 实现侧滑菜单及其主体部分上下滑动的方法
2018/01/25 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
JavaScript实现点击出现图片并统计点击次数功能示例
2018/07/23 Javascript
Express 配置HTML页面访问的实现
2020/11/01 Javascript
vue.js+element 默认提示中英文操作
2020/11/11 Javascript
纯Python开发的nosql数据库CodernityDB介绍和使用实例
2014/10/23 Python
简单介绍Python中的try和finally和with方法
2015/05/05 Python
举例讲解Python设计模式编程中对抽象工厂模式的运用
2016/03/02 Python
Python实现数据库并行读取和写入实例
2017/06/09 Python
Python自动化运维_文件内容差异对比分析
2017/12/13 Python
Python装饰器原理与简单用法实例分析
2018/04/29 Python
python实现QQ批量登录功能
2019/06/19 Python
日本著名的服饰鞋帽综合类购物网站:MAGASEEK
2019/01/09 全球购物
SNIDEL官网:日本VIVI杂志人气少女第一品牌
2020/03/12 全球购物
俄罗斯最大的香水和化妆品网上商店:Randewoo
2020/11/05 全球购物
说说你所熟悉或听说过的j2ee中的几种常用模式?及对设计模式的一些看法
2012/05/24 面试题
税务会计岗位职责
2014/02/18 职场文书
医学求职自荐信
2014/06/21 职场文书
银行进社区活动总结
2014/07/07 职场文书
公司员工辞职信范文
2015/05/12 职场文书
走近毛泽东观后感
2015/06/04 职场文书
运动会800米赞词
2015/07/22 职场文书
学校隐患排查制度
2015/08/05 职场文书
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2021/06/21 Java/Android
Python爬取用户观影数据并分析用户与电影之间的隐藏信息!
2021/06/29 Python
一起来学习Python的元组和列表
2022/03/13 Python
十大最强电系宝可梦,阿尔宙斯电系之一,第七被称为雷神
2022/03/18 日漫
教你nginx跳转配置的四种方式
2022/07/07 Servers