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 相关文章推荐
清空上传控件input file的值
Jul 03 Javascript
js this函数调用无需再次抓获id,name或标签名
Mar 03 Javascript
元素未显示设置width/height时IE中使用currentStyle获取为auto
May 04 Javascript
Python脚本后台运行的几种方式
Mar 09 Javascript
AngularJS入门教程之AngularJS表达式
Apr 18 Javascript
vue-router3.0版本中 router.push 不能刷新页面的问题
May 10 Javascript
浅谈Webpack 是如何加载模块的
May 24 Javascript
Vue路由钩子之afterEach beforeEach的区别详解
Jul 15 Javascript
JavaScript之数组扁平化详解
Jun 03 Javascript
js判断浏览器的环境(pc端,移动端,还是微信浏览器)
Dec 24 Javascript
原生js实现点击轮播切换图片
Feb 11 Javascript
js+html+css实现手动轮播和自动轮播
Dec 30 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
在Windows系统下使用PHP生成Word文档的教程
2015/07/03 PHP
详解PHP的Laravel框架中Eloquent对象关系映射使用
2016/02/26 PHP
Zend Framework+smarty用法实例详解
2016/03/19 PHP
yii2.0整合阿里云oss上传单个文件的示例
2017/09/19 PHP
jquery animate 动画效果使用说明
2009/11/04 Javascript
读jQuery之七 判断点击了鼠标哪个键的代码
2011/06/21 Javascript
javascript window.open打开新窗口后无法再次打开该窗口问题的解决方法
2014/04/12 Javascript
js类定义函数时用prototype与不用的区别示例介绍
2014/06/10 Javascript
JQuery中使文本框获得焦点的方法实例分析
2015/02/28 Javascript
jQuery如何使用自动触发事件trigger
2015/11/29 Javascript
javascript创建含数字字母的随机字符串方法总结
2016/08/01 Javascript
用headjs来管理和加载js 提高网站加载速度
2016/11/29 Javascript
js自定义QQ菜单效果
2017/01/10 Javascript
图片上传之FileAPI与NodeJs
2017/01/24 NodeJs
JavaScript中捕获与冒泡详解及实例
2017/02/03 Javascript
详解JavaScript 中getElementsByName在IE中的注意事项
2017/02/21 Javascript
angular.js指令中transclude选项及ng-transclude指令详解
2017/05/24 Javascript
node中使用es6/7/8(支持性与性能)
2019/03/28 Javascript
微信小程序实现导航栏和内容上下联动功能代码
2020/06/29 Javascript
[38:32]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第二局
2016/02/26 DOTA
[01:21]2018DOTA2亚洲邀请赛4.5采访 打DOTA2也能有女朋友?
2018/04/06 DOTA
在Python3中初学者应会的一些基本的提升效率的小技巧
2015/03/31 Python
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
python的构建工具setup.py的方法使用示例
2017/10/23 Python
PyCharm+Qt Designer+PyUIC安装配置教程详解
2019/06/13 Python
Python MOCK SERVER moco模拟接口测试过程解析
2020/04/13 Python
面向新手解析python Beautiful Soup基本用法
2020/07/11 Python
python+requests实现接口测试的完整步骤
2020/10/27 Python
使用Python爬取小姐姐图片(beautifulsoup法)
2021/02/11 Python
CSS3实现点击放大的动画实例代码
2017/02/27 HTML / CSS
小米乌克兰网上商店:Xiaomi.UA
2019/10/29 全球购物
财务总经理岗位职责
2014/02/16 职场文书
酒店员工检讨书
2014/02/18 职场文书
总结表彰大会主持词
2014/03/26 职场文书
大学毕业典礼致辞
2015/07/29 职场文书
python 遍历磁盘目录的三种方法
2021/04/02 Python