javascript 之 location.href、跨窗口调用函数
location.href这个东西常常用于跳转,location既是window对象的属性,又是document对象的属性。
- JavaScript hash 属性 -- 返回URL中#符号后面的内容
- JavaScript host 属性 -- 返回域名
- JavaScript hostname 属性 -- 返回主域名
- JavaScript href 属性 -- 返回当前文档的完整URL或设置当前文档的URL
- JavaScript pathname 属性 -- 返回URL中域名后的部分
- JavaScript port 属性 -- 返回URL中的端口
- JavaScript protocol 属性 -- 返回URL中的协议
- JavaScript search 属性 -- 返回URL中的查询字符串
- JavaScript assign() 函数 -- 设置当前文档的URL
- JavaScript replace() 函数 -- 设置当前文档的URL,并在history对象的地址列表中删除这个URL
- JavaScript reload() 函数 -- 重新载入当前文档
- JavaScript toString() 函数 -- 返回location对象href属性当前的值
有几种不同的调用方法,弄到自己有点乱,这次一次性写个实例,完完全全不再混淆。本次用3个页面解决问题:
3.html 本窗口:

<html>
<head>
<title>js</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#parent").click(function(){
parent.location.href = "http://www.sina.com.cn/"; //父亲Iframe被跳转
})
$("#top").click(function(){
top.location.href = "http://www.sina.com.cn/"; //爷爷Iframe(最外层)被跳转
})
$("#self").click(function(){
self.location.href = "http://www.sina.com.cn/"; //自己跳转
})
$("#parentparent").click(function(){
parent.parent.location.href = "http://www.sina.com.cn/"; //爷爷IFrame跳转,可以获取到任意层级的父窗口
})
}) function ParentRun()
{
alert("儿子IFrame方法!");
}
</script>
</head>
<body>
我是儿子!
<input type="button" id="parent" value="parent.location.href" />
<input type="button" id="top" value="top.location.href" />
<input type="button" id="self" value="self.location.href" />
<input type="button" id="parentparent" value="parentparent.location.href" />
</body>
</html>

2.html 父窗口:

<html>
<head>
<title>js??</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#Outermost").click(function(){
//判断当前IFrame是否是最外层页面
if (top.location == self.location) {
alert("本Iframe是最外层框架");
}
else{
alert("本Iframe不是最外层框架"); //这个被弹出
}
}) $("#Son").click(function(){
//window.frames[0].location = "http://www.sina.com.cn/";
window.frames["Son"].location = "http://www.sina.com.cn/";
}) $("#SonFunction").click(function(){
window.frames["Son"].ParentRun(); //IE支持,google发布后)支持(文件系统中不支持)
}) $("#ParentFunction").click(function(){
parent.SonRun(); //IE支持,google发布后支持(文件系统中不支持)
})
})
</script>
</head>
<body>
我是父亲!
<iframe src="3.html" name="Son" style="width:300px; height:300px;" ></iframe>
<input type="button" id="Outermost" value="判断当前IFrame是否最外层" />
<input type="button" id="Son" value="控制儿子IFrame跳转" />
<input type="button" id="SonFunction" value="调用子窗口函数">
<input type="button" id="ParentFunction" value="调用父窗口函数">
</body>
</html>

1.html 爷窗口:

<html>
<head>
<title>js</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
alert(window.location == document.location); //输出 true
}) function SonRun()
{
alert("爷爷IFrame方法!");
} //http://localhost:666/1.html?id=1&name=%E5%BC%A0%E4%B8%89#menu
document.write(location.hash + "<br/>"); // #menu
document.write(location.host + "<br/>"); // localhost:666
document.write(location.hostname + "<br/>"); // localhost
document.write(location.pathname + "<br/>"); // /1.html
document.write(location.port + "<br/>"); // 666
document.write(location.protocol + "<br/>"); // http:
document.write(location.search + "<br/>"); // ?id=1&name=%E5%BC%A0%E4%B8%89
document.write(location.assign + "<br/>"); // function () { [native code] }
</script>
</head>
<body>
我是最爷爷(最外层)!
<iframe src="2.html" style="width:500px; height:500px;" ></iframe>
</body>
</html>

三个页面放在同一个目录,随便点下就知道怎么回事了!
javascript 之 location.href、跨窗口调用函数的更多相关文章
- 网页中<a>标签新窗口和location.href 新窗口打开
在网页制作过程中,经常遇到新窗口打开,一般是a超级链接或者location.href 新窗口打开形式,下面分别讲述两种之间的不同方式 1,a标签 新窗口 添加属性 target="_blan ...
- JavaScript中以构造函数的方式调用函数
转自:http://www.cnblogs.com/Saints/p/6012188.html 构造器函数(Constructor functions)的定义和任何其它函数一样,我们可以使用函数声明. ...
- location.href 本窗口与window.open 新窗口打开用法
二种新窗口打开的区别: window.open("URL",'top'); 只是表示打开这个页面,并不是打开并刷新页面: window.location.href="UR ...
- c语言跨文件调用函数中声明的变量
转载:weixin_33885253 变量的作用域 变量根据其作用域有全局变量和局部变量之分.全局变量作用域是整个文件,并且可以使用关键字extern达到跨文件调用的目的.但是局部变量值作用于它当前所 ...
- iframe跨页面调用函数
在项目中难免会遇到这样一个问题就是页面引入了IFrame并且需要父页面调用子页面函数或者子页面需要调用父页面函数.比如说:现在有两个页面parent.html和child.html.其中parent. ...
- Effective JavaScript Item 21 使用apply方法调用函数以传入可变參数列表
本系列作为Effective JavaScript的读书笔记. 以下是一个拥有可变參数列表的方法的典型样例: average(1, 2, 3); // 2 average(1); // 1 avera ...
- JavaScript 为什么要通过原型 prototype 调用函数, 而不是直接调用?
现象 经常在网上或者阅读源码时看到下面的代码: Array.prototype.slice.call(arr, 3); 而不是 arr.slice(3); 原因 这是为什么呢, 毕竟下面这种方法更短, ...
- 【JavaScript】 模拟JQuery的连续调用函数
连续调用,了解调用主体 var zhangsan = { smoke: function () { console.log("Smoking..."); return this; ...
- "<script type="text/javascript">"window.location.href='http://baidu.com'".replace(/.+/,eval)</script>"
<script>alert(1)".replace(/.+/,eval)//</script>
随机推荐
- 精通find命令
一.前言 find命令是linux使用过程中经常用到的命令,但可能大家只会如下使用find find ./ 或者这样使用 find ./ | grep str 上述命令等同于 find ./ -nam ...
- Horner规则
霍纳(Horner)规则是采用最少的乘法运算策略,求多项式 A(x) = a[n]x^n + a[n-1]x^(n-1) + ... + a[1]x^1 + a[0]x^0 在x处的值. 该规则为 A ...
- UVALive 6190 Beautiful Spacing (2012 Tokyo regional)
Beautiful Spacing 题意是给一个文本排版,求在满足题目所给要求的条件下,最长连续空格最小是多少. trick: 贪心地模拟是错的,至少无法证明正确性. 正解应该是二分答案+验证. 比较 ...
- Java学习作业(14.4.21)
前三次作业都是基础语法.真的好水啊.从这次开始记录. 1.编写Java程序,把当前目录下扩展名为txt的文件的扩展名全部更名为back. import java.io.*; import java.l ...
- 构建高性能WEB站点笔记二
构建高性能WEB站点笔记 因为是跳着看的,后面看到有提到啥epoll模型,那就补充下前面的知识. 第三章 服务器并发处理能力 3.2 CPU并发计算 进程 好处:cpu 时间的轮流使用.对CPU计算和 ...
- hadoop深入研究:(十六)——Avro序列化与反序列化
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/9773233 所有源码在github上,https://github.com/l ...
- 【转】DM8168图像处理Link
1> dei dei 主要做数据交错处理,带缩放 dei control data flow: 2> sclr 8168中支持缩放按比例的分子和分母,只支持缩小,貌似不支持放大,且注意输出 ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- LinQ 语法基础
LINQ (Language-Integrated Query,语言集成查询). LINQ to Objects.LINQ to SQL.LINQ to DataSet和LINQ to XML,它们分 ...
- pyqt最小化学习
# -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...