location和window.open的小结--(获取url里的各种玩意以及页面)

来源:互联网 发布:淘宝店策划方案 编辑:程序博客网 时间:2024/05/21 17:10

window.location的样子

示例
URL:http://d.a.com:9000/index.html?name=ddd&when=2017#first

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

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的窗口 
(每个窗口都可以给他命名) 
不能为省略,可以为空!


获取url中携带参数的方法

/*返回一个对象,包含url中所有参数*/
const getRequest = function() {let url = window.location.search; //获取url中"?"符后的字串let theRequest = new Object();if (url.indexOf("?") != -1) {let str = url.substr(1);if (str.indexOf("&") != -1) {let strs = str.split("&");for (let i = 0; i < strs.length; i++) {theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);}} else {theRequest[str.split("=")[0]] = unescape(str.split("=")[1]);}}return theRequest;}
/*根据要获取的参数名称,返回url中对应参数值*/(name)=> {        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');        var r = decodeURIComponent(window.location.search.substr(1)).match(reg);        if (r != null) return unescape(r[2]);        return null;

}

                                             
0 0
原创粉丝点击