window.location

window.location 对象所包含的属性

属性描述
hash 从井号 (#) 开始的 URL(锚)
host 主机名和当前 URL 的端口号
hostname 当前 URL 的主机名
href 完整的 URL
pathname 当前 URL 的路径部分
port 当前 URL 的端口号
protocol 当前 URL 的协议
search 从问号 (?) 开始的 URL(查询部分)

 

 

 

 

 

 

 

 

 

 

  • protocol 返回地址的协议,取值为 'http:','https:','file:' 等等。
  • hostname 返回地址的主机名,例如,一个“http://www.microsoft.com/china/”的地址,location.hostname == 'www.microsoft.com'。
  • port 返回地址的端口号,一般 http 的端口号是 '80'。
  • host 返回主机名和端口号,如:'www.a.com:8080'。
  • pathname 返回路径名,如“http://www.a.com/b/c.html”,location.pathname == 'b/c.html'。
  • hash 返回“#”以及以后的内容,如“http://www.a.com/b/c.html#chapter4”,location.hash == '#chapter4';如果地址里没有“#”,则返回空字符串。
  • search 返回“?”以及以后的内容,如“http://www.a.com/b/c.asp?selection=3&jumpto=4”,location.search == '?selection=3&jumpto=4';如果地址里没有“?”,则返回空字符串。
  • href 返回以上全部内容,也就是说,返回整个地址。在浏览器的地址栏上怎么显示它就怎么返回。如果想一个窗口对象打开某地址,可以使用“location.href = '...'”,也可以直接用“location = '...'”来达到此目的。

 

window.location.hash

要使用 JS 定位锚点,完全可以使用 window.hash 配合元素 ID 完成。比如快速定位到页面的某条评论,则直接使用如下代码即可:

window.location.hash = "#comment-5981";

另外 Twitter、Facebook、Google 等已经开始大量使用 #! 这种形式的 hash 方法处理异步交互页面的 URL 可回溯功能。

window.location.search

如果有这样一个 URL 地址:

http://www.google.com.hk/search?hl=zh-CN&source=hp&biw=1400&bih=935&q=%E8%8A%92%E6%9E%9C%E5%B0%8F%E7%AB%99&aq=f&aqi=&aql=&oq=

如何利用 JS 脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得,然后通过 split 方法结合循环遍历自由组织数据格式。

另外,如果根据用户的搜索条件刷新页面,只需直接设置 window.location.search 即可。

 

 

方法概览

 

 

  • reload() 相当于按浏览器上的“刷新”(IE)或“Reload”(Netscape)键。
  • replace() 打开一个 URL,并取代历史对象中当前位置的地址。用这个方法打开一个 URL 后,按下浏览器的“后退”键将不能返回到刚才的页面。

 

 

二、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";

}

posted @ 2015-02-09 13:29  eaysun  阅读(249)  评论(0编辑  收藏  举报