javascript中的location用法简单介绍


Posted in Javascript onMarch 07, 2007

先前写了一片用window.location.href实现刷新另个框架页面 ,特此我看了一下locaiton的详细用法,对此有点改进,现在我将他整理成js,方便查阅,也贴上和朋友们分享一下,具体如下:

第一、简单介绍一下location属性、用法以及相关示例:
Location
包含了关于当前 URL 的信息。

描述
location 对象描述了与一个给定的 Window 对象关联的完整 URL。location 对象的每个属性都描述了 URL 的不同特性。
通常情况下,一个 URL 会有下面的格式:

协议//主机:端口/路径名称#哈希标识?搜索条件 例如:

http://skylaugh.cnblogs.com/index.html#topic1?x=7&y=2 这些部分是满足下列需求的:

“协议”是 URL 的起始部分,直到包含到第一个冒号。 
“主机”描述了主机和域名,或者一个网络主机的 IP 地址。
“端口”描述了服务器用于通讯的通讯端口。 
路径名称描述了 URL 的路径方面的信息。
“哈希标识”描述了 URL 中的锚名称,包括哈希掩码(#)。此属性只应用于 HTTP 的 URL。 
“搜索条件”描述了该 URL 中的任何查询信息,包括问号。此属性只应用于 HTTP 的 URL。“搜索条件”字符串包含变量和值的配对;每对之间由一个“&”连接。 

属性概览
hash: Specifies an anchor name in the URL. 
host: Specifies the host and domain name, or IP address, of a network host.  
hostname: Specifies the host:port portion of the URL.  
href: Specifies the entire URL.  
pathname: Specifies the URL-path portion of the URL.  
port: Specifies the communications port that the server uses.  
protocol: Specifies the beginning of the URL, including the colon.  
search: Specifies a query. 

方法概览
reload Forces a reload of the window's current document.  
replace Loads the specified URL over the current history entry.  

主要功能示例,其他类同:
hash:

newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.hash = #59831 

host
A string specifying the server name, subdomain, and domain name.
newWindow.location.href =   http://skylaugh.cnblogs.com
newWindow.location.host = skylaugh.cnblogs.com

href
A string specifying the entire URL.

window.location.href="http://home.netscape.com/"

pathname
A string specifying the URL-path portion of the URL.

search
A string beginning with a question mark that specifies any query information in the URL.

newWindow.location.href = http://skylaugh.cnblogs.com
newWindow.location.search = ?newsid=111 

二、location之页面跳转js如下:
//简单跳转
function gotoPage(url)
{
// eg. var url = "newsview.html?catalogid="+catalogID+"&pageid="+pageid;
window.location = url;
}
// 对location用法的升级,为单个页面传递参数
function goto_catalog(iCat)
{
if(iCat<=0)
{
top.location = "../index.aspx"; // top出去
}
else
{
window.location = "../newsCat.aspx?catid="+iCat;
}
}
// 对指定框架进行跳转页面,二种方法皆可用
function goto_iframe(url)
{
parent.mainFrame.location = "../index.aspx"; //
// parent.document.getElementById("mainFrame").src = "../index.aspx";// use dom to change page // 同时我增加了dom的写法
}
// 对指定框架进行跳转页面,因为 parent.iframename.location="../index.aspx"; 方法不能实行,主要是 "parent.iframename" 中的iframename在js中被默认为节点,而不能把传递过来的参数转换过来,所以用dom实现了该传递二个参数的框架跳转页面,希望那位仁兄不吝赐教!
function goto_iframe(iframename,url) 
{
parent.document.getElementById(iframename).src = "../index.aspx";// use dom to change page by iframeName

//}
// 回到首页
function gohome()
{
top.location = "/index.aspx";
}
</script>

Javascript 相关文章推荐
ajaxControlToolkit AutoCompleteExtender的用法
Oct 30 Javascript
无阻塞加载脚本分析[全]
Jan 20 Javascript
通过下拉框的值来确定输入框是否可以为空的代码
Oct 18 Javascript
jquery实现table鼠标经过变色代码
Sep 25 Javascript
js window.open弹出新的网页窗口
Jan 16 Javascript
jQuery+jRange实现滑动选取数值范围特效
Mar 14 Javascript
jQuery查找节点方法完整实例
Sep 13 Javascript
浅谈vue引入css,less遇到的坑和解决方法
Jan 20 Javascript
React props和state属性的具体使用方法
Apr 12 Javascript
微信小程序中使用ECharts 异步加载数据实现图表功能
Jul 13 Javascript
fetch 如何实现请求数据
Dec 20 Javascript
详解js location.href和window.open的几种用法和区别
Dec 02 Javascript
JsEasy简介 JsEasy是什么?与下载
Mar 07 #Javascript
动态控制Table的js代码
Mar 07 #Javascript
js+FSO遍历文件夹下文件并显示
Mar 07 #Javascript
学习js所必须要知道的一些
Mar 07 #Javascript
修改发贴的编辑功能
Mar 07 #Javascript
Javascript之文件操作
Mar 07 #Javascript
得到文本框选中的文字,动态插入文字的js代码
Mar 07 #Javascript
You might like
做个自己站内搜索引擎
2006/10/09 PHP
php+mysql实现无限分类实例详解
2015/01/15 PHP
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
2015/03/18 PHP
php函数连续调用实例分析
2015/07/30 PHP
PHP函数func_num_args用法实例分析
2015/12/07 PHP
PHP中Array相关函数简介
2016/07/03 PHP
Avengerls vs KG BO3 第一场2.18
2021/03/10 DOTA
jquery监控数据是否变化(修正版)
2011/04/12 Javascript
jQuery使用andSelf()来包含之前的选择集
2014/05/19 Javascript
jquery操作checked属性以及disabled属性的多种方法
2014/06/20 Javascript
jQuery固定浮动侧边栏实现思路及代码
2014/09/28 Javascript
第一次记录Bootstrap table学习笔记(1)
2017/05/18 Javascript
php 修改密码实现代码
2017/05/24 Javascript
vue中v-for加载本地静态图片方法
2018/03/03 Javascript
11行JS代码制作二维码生成功能
2018/03/09 Javascript
原生js通过一行代码实现简易轮播图
2019/06/05 Javascript
vue新建项目并配置标准路由过程解析
2019/12/09 Javascript
Vue使用Ref跨层级获取组件的步骤
2021/01/25 Vue.js
[01:14:05]《加油DOTA》第四期
2014/08/25 DOTA
[02:19]DOTA2上海特级锦标赛 观赛指南 Spectator Guide
2016/02/04 DOTA
[39:21]LGD vs OG 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.24
2019/09/10 DOTA
Python使用tkinter库实现文本显示用户输入功能示例
2018/05/30 Python
Selenium元素的常用操作方法分析
2018/08/10 Python
浅谈python中拼接路径os.path.join斜杠的问题
2018/10/23 Python
Python反爬虫技术之防止IP地址被封杀的讲解
2019/01/09 Python
详解Python函数式编程—高阶函数
2019/03/29 Python
python实现爬虫抓取小说功能示例【抓取金庸小说】
2019/08/09 Python
Python 切分数组实例解析
2019/11/07 Python
印尼综合在线预订网站:Tiket.com(机票、酒店、火车、租车和娱乐)
2018/10/11 全球购物
Douglas意大利官网:购买香水和化妆品
2020/05/27 全球购物
前台接待的工作职责
2013/11/21 职场文书
创优争先心得体会
2014/09/11 职场文书
党员应该树立反腐倡廉的坚定意识思想汇报
2014/09/12 职场文书
2015应届毕业生自荐信范文
2015/03/05 职场文书
2015年毕业生个人自荐书
2015/03/24 职场文书
2019年二手房买卖合同范本
2019/10/14 职场文书