window.location 小结)

其实在网上都能找到,我只是总结一下,方便自己查找使用

示例
URL:http://b.a.com:88/index.php?name=kang&when=2011#first

属性     含义 
protocol 协议  "http:"
hostname 服务器的名字 "b.a.com"
port 端口 "88"
pathname URL中主机名后的部分 "/index.php"
search "?"后的部分,又称为查询字符串 "?name=kang&when=2011"
hash 返回"#"之后的内容 "#first"
host 等于hostname + port "b.a.com:88"
href 当前页面的完整URL  "http://www.a.com:88/index.php?name=kang&when=2011#first"

window.location和document.location互相等价的,可以交换使用

location的8个属性都是可读写的,但是只有href与hash的写才有意义。

例如改变location.href会重新定位到一个URL,而修改location.hash会跳到当前页面中的anchor(<a id="name">或者<div id="id">等)名字的标记(如果有),而且页面不会被重新加载

注意
URL:http://b.a.com:88/index.php?name=kang&how=#when=2011#first

search: "?name=kang&how=" 第一个"?"之后
hash: "#when=2011#first" 第一个"#"之后的内容
方法
location.assign( url )
location.assign(‘http://www.baidu.com‘); 等同于 window.location = ‘http://www.baidu.com‘
这种方式会讲新地址放到浏览器历史栈中,意味着转到新页面后“后退按钮”仍可以回到该页面。
location.replace( url )
与assign方法一样,但会从浏览器历史栈中删除本页面,也就是说跳转到新页面后“后退按钮”不能回到该页面。目前IE、Chrome只是简单的跳转,只有Firefox会删除本页面的历史记录。
location.reload( force )
重新载入当前页面。force为true时从服务器端重载;false则从浏览器缓存中重载,默认值false。

location.hash

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

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

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 即可。

windows.open("URL","窗口名称","窗口外观设定");

<A href="javascript:window.open(‘webpage.asp‘,‘_self‘)"> 点击这里 </A>

<a onclick="window.open(‘webpage.asp‘,‘_self‘);void 0" href="#"> 点击这里 </a>

于Window.open的参数小结(参书改编) 先看一个例子:
[javascipt:]window.open("Webpage.asp?",Derek,"height=100,width=100,status=yes,toolbar=yes, menubar=no,location=no");
此语句打开一个新窗口,页面为webpage.asp,参数为var,名字为Derek,高为100,宽为100,显示状态栏和工具条,不显示菜单和地址。

具体总结的各个属性参数如下:
window = object.open([URL ][, name ][, features ][, replace]]]])
URL:新窗口的URL地址
name:新窗口的名称,可以为空
featurse:属性控制字符串,在此控制窗口的各种属性,属性之间以逗号隔开。
fullscreen= { yes/no/1/0 } 是否全屏,默认no

channelmode= { yes/no/1/0 } 是否显示频道栏,默认no
toolbar= { yes/no/1/0 } 是否显示工具条,默认no
location= { yes/no/1/0 } 是否显示地址栏,默认no
directories = { yes/no/1/0 } 是否显示转向按钮,默认no
status= { yes/no/1/0 } 是否显示窗口状态条,默认no
menubar= { yes/no/1/0 } 是否显示菜单,默认no
scrollbars= { yes/no/1/0 } 是否显示滚动条,默认yes
resizable= { yes/no/1/0 } 是否窗口可调整大小,默认no
width=number 窗口宽度(像素单位)
height=number 窗口高度(像素单位)
top=number 窗口离屏幕顶部距离(像素单位)
left=number 窗口离屏幕左边距离(像素单位)

关于本题中,第二个参数的详解:
name可以为:

_blank 表示新开一个窗口,
_parent表示父框架窗口,
_self表示覆盖该窗口,
xxx表示覆盖名字为xxx的窗口
(每个窗口都可以给他命名)
不能为省略,可以为空!

时间: 2024-08-01 07:33:05

window.location 小结)的相关文章

关于js中window.location.href,location.href,parent.location.href,top.location.href的用法

关于js中window.location.href,location.href,parent.location.href,top.location.href的用法 "window.location.href"."location.href"是本页面跳转. "parent.location.href" 是上一层页面跳转. "top.location.href" 是最外层的页面跳转. 举例说明: 如果A,B,C,D都是html,D

window.location.hash的简单了解

location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location.hash则可以用来获取或设置页面的标签值.比如http://domain/#admin的location.hash="#admin".利用这个属性值可以做一个非常有意义的事情. 很多人都喜欢收藏网页,以便于以后的浏览.不过对于Ajax页面来说的话,一般用一个页面来处理所有的事务,也就是说,

window.location.href和window.location.replace的区别

在页面中逐级进行点击请求以下页面:a.html->b.html->c.html window.location.href 做跳转 window.history.go(-1);window.history.back(); 方法时,会向服务器进行请求,根据服务器记录的请求进行跳转,因此会正确返回对应的页面a.html. window.location.replace 做跳转 window.history.go(-1);window.history.back(); 方法时,不会向服务器进行请求,因此

关于js中window.location.href,location.href,parent.location.href,top.location.href用法

"window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一层页面跳转 "top.location.href"是最外层的页面跳转 举例说明: 如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写 "window.location.href"."locatio

window.location.href的用法

在写ASP.Net程序的时候,我们经常遇到跳转页面的问题,我们经常使用Response.Redirect 做ASP.NET框架页跳转,如果客户要在跳转的时候使用提示,这个就不灵光了,如: Response.Write("< script>alert('恭喜您,注册成功!');< /script>");   Response.Redirect("main.html");    这时候我们的提示内容没有出来就跳转了,和Response.Redir

fixed window.location.origin useless in IE

Today I got a issue that window.location.origin returns undefined in IE 9 . But we can fix this issue with following code: if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + window.location.hostname + (wind

JavaScript window.location对象

JavaScript window.location对象 示例 注意 方法 经常使用window.location,它的结构总是记不住,简单梳理下,方便以后查询. 示例 URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性 含义 值 protocol: 协议 "http:" hostname: 服务器的名字 "b.a.com" port: 端口 "88" pathname

window location href 刷新

页面刷新类型: 1.window.location.reload 刷新. window.location.reload()与window.location.href=window.location.href都可以刷新页面. 注: window.location.reload()刷新页面时,页面若有数据提交会提示是否提交数据,就是我们经常看到的那个讨厌的提示框.window.location.href=window.location.href则不会. 2.frame框架刷新. 代码如下: <htm

js中window.location.search的用法和作用

用该属性获取页面 URL 地址: window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostname 当前 URL 的主机名 href 完整的 URL pathname 当前 URL 的路径部分 port 当前 URL 的端口号 protocol 当前 URL 的协议 search 从问号 (?) 开始的 URL(查询部分) 如图就是取出url中的参数. js 脚本捕获页面 GET 方式请求的参