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 相关文章推荐
硬盘浏览程序,保存成网页格式便可使用
Dec 03 Javascript
FLASH 广告之外的链接
Dec 16 Javascript
Js 获取当前日期时间及其它操作实现代码
Mar 04 Javascript
Javascript操作URL函数修改版
Nov 07 Javascript
关于js中for in的缺陷浅析
Dec 02 Javascript
JavaScript数值转换的三种方式总结
Jul 31 Javascript
jQuery实用技巧必备(中)
Nov 03 Javascript
理解Javascript图片预加载
Feb 23 Javascript
基于jquery编写的放大镜插件
Mar 23 Javascript
jQuery代码实现对话框右上角菜单带关闭×
May 03 Javascript
echarts3 使用总结(绘制各种图表,地图)
Jan 05 Javascript
node脚手架搭建服务器实现token验证的方法
Jan 20 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
整合了前面的PHP数据库连接类~~做成一个分页类!
2006/11/25 PHP
解决文件名解压后乱码的问题 将文件名进行转码的代码
2012/01/10 PHP
php获取301跳转URL简单实例
2013/12/16 PHP
PHP中shuffle数组值随便排序函数用法
2014/11/21 PHP
php魔术方法功能与用法实例分析
2016/10/19 PHP
php写入mysql中文乱码的实例解决方法
2019/09/17 PHP
Vue.js一个文件对应一个组件实践
2016/10/27 Javascript
基于vuejs实现一个todolist项目
2017/04/11 Javascript
web前端开发中常见的多列布局解决方案整理(一定要看)
2017/10/15 Javascript
详解webpack中的hash、chunkhash、contenthash区别
2018/01/05 Javascript
vue2.0.js的多级联动选择器实现方法
2018/02/09 Javascript
Vuex 在Vue 组件中获得Vuex 状态state的方法
2018/08/27 Javascript
jQuery-ui插件sortable实现自由拖动排序
2018/12/01 jQuery
vue项目在线上服务器访问失败原因分析
2020/08/14 Javascript
详解webpack的clean-webpack-plugin插件报错
2020/10/16 Javascript
Python 元组(Tuple)操作详解
2014/03/11 Python
wxPython事件驱动实例详解
2014/09/28 Python
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
深入分析python数据挖掘 Json结构分析
2018/04/21 Python
python版本的仿windows计划任务工具
2018/04/30 Python
uwsgi+nginx部署Django项目操作示例
2018/12/04 Python
python将一个英文语句以单词为单位逆序排放的方法
2018/12/20 Python
kafka-python批量发送数据的实例
2018/12/27 Python
python钉钉机器人运维脚本监控实例
2019/02/20 Python
Python : turtle色彩控制实例详解
2020/01/19 Python
Tensorflow 定义变量,函数,数值计算等名字的更新方式
2020/02/10 Python
css3实例教程 一款纯css3实现的环形导航菜单
2014/10/20 HTML / CSS
解释一下Windows的消息机制
2014/01/30 面试题
PHP引擎php.ini参数优化深入讲解
2021/03/24 PHP
四年级数学教学反思
2014/02/02 职场文书
单位绩效考核方案
2014/05/11 职场文书
安全责任书模板
2014/07/22 职场文书
领导班子遵守党的政治纪律情况对照检查材料
2014/09/26 职场文书
2015年推普周活动方案
2015/05/06 职场文书
财产分割协议书
2016/03/22 职场文书
Tomcat配置访问日志和线程数
2022/05/06 Servers