详解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 相关文章推荐
JQuery上传插件Uploadify使用详解及错误处理
Apr 27 Javascript
两种常用的javascript数组去重方法思路及代码
Mar 26 Javascript
thinkphp中常用的系统常量和系统变量
Mar 05 Javascript
js实现非常简单的焦点图切换特效实例
May 07 Javascript
延时加载JavaScript代码提高速度
Dec 27 Javascript
js实现页面跳转的五种方法推荐
Mar 10 Javascript
Bootstrap按钮组件详解
Apr 26 Javascript
BootStrap中Table隐藏后显示问题的实现代码
Aug 31 Javascript
react 父组件与子组件之间的值传递的方法
Sep 14 Javascript
js实现倒计时器自定义时间和暂停
Feb 25 Javascript
Vue插件之滑动验证码
Sep 21 Javascript
js基础语法与maven项目配置教程案例
Jul 15 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读MYSQL中文乱码的解决方法
2006/12/17 PHP
zend framework多模块多布局配置
2011/02/26 PHP
php-msf源码详解
2017/12/25 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
2018/05/30 PHP
php常用字符串查找函数strstr()与strpos()实例分析
2019/06/21 PHP
网页的标准,IMG不支持onload标签怎么办
2006/06/29 Javascript
jQuery hover 延时器实现代码
2011/03/12 Javascript
JS等比例缩小图片尺寸的实例
2013/02/27 Javascript
在Linux上用forever实现Node.js项目自启动
2014/07/09 Javascript
jquery实现类似EasyUI的页面布局可改变左右的宽度
2020/09/12 Javascript
JS基于正则截取替换特定字符之间字符串操作示例
2017/02/03 Javascript
Angularjs 动态添加指令并绑定事件的方法
2017/04/13 Javascript
JavaScript监听手机物理返回键的两种解决方法
2017/08/14 Javascript
详解VueRouter进阶之导航钩子和路由元信息
2017/09/13 Javascript
vue组件表单数据回显验证及提交的实例代码
2018/08/30 Javascript
微信小程序实现文字无限轮播效果
2018/12/28 Javascript
js简单遍历获取对象中的属性值的方法示例
2019/06/19 Javascript
JS数组转字符串实现方法解析
2020/09/04 Javascript
python 示例分享---逻辑推理编程解决八皇后
2014/07/20 Python
在Python中使用pngquant压缩png图片的教程
2015/04/09 Python
wxPython中listbox用法实例详解
2015/06/01 Python
玩转python爬虫之cookie使用方法
2016/02/17 Python
详解python算法之冒泡排序
2019/03/05 Python
python爬虫 urllib模块url编码处理详解
2019/08/20 Python
ansible动态Inventory主机清单配置遇到的坑
2020/01/19 Python
django model 条件过滤 queryset.filter(**condtions)用法详解
2020/05/20 Python
matplotlib绘制多子图共享鼠标光标的方法示例
2021/01/08 Python
Zooplus葡萄牙:欧洲领先的网上宠物商店
2018/07/01 全球购物
syb养殖创业计划书
2014/01/09 职场文书
大学生自我鉴定评语
2014/01/27 职场文书
工作决心书范文
2014/03/11 职场文书
新农村建设典型材料
2014/05/31 职场文书
公务员政审材料
2014/12/23 职场文书
公司市场部岗位职责
2015/04/15 职场文书
永不妥协观后感
2015/06/10 职场文书
小学秋季运动会加油口号及加油稿
2019/08/19 职场文书