代码改变世界

window对象的方法属性

2017-09-02 19:40  卖蟑螂的小男孩  阅读(207)  评论(0编辑  收藏  举报

方法

1、window.open(URL, name, ‘width=100,height=100,left=0,top=0’)

打开一个新窗口,可指定地址(URL),指定新窗口title(name,我试了没反应啊),

此外还可以设置新窗口打开的位置,大小等。left、top、width、height(当未设置宽高时left、top谷歌不起作用,ie8以上设置宽或高即可)

2、window.close()

关闭本窗口

3、window.history.back()

回到上一页

 

属性

1、window.location属性

示例:http://xxx.com:8080/index.html?id=1&name=周杰伦#abc

属性 含义
href: 获取当前完整的URL值 "http://xxx.com:8080/index.html?id=1&name=周杰伦#abc"
protocol: 获取协议 "http:"
hostname: 服务器名字 "xxx.com"
port: 端口 "8080"
pathname: URL端口后的部分 "index.html"
search: ?后的查询字符串 "id=1&name=周杰伦"
hash: #之后的内容   "abc"
host: hostname + port "xxx.com:8080"

 

 

 

 

 

 

 

 

 

  • 在使用window.location.xx属性时,可简写为location.xx
  • window.location 等价于document.location
  • 但是document.open() 与 window.open()不一样,一个是打开新窗口,一个是在此document内。

在获取location.search时,往往需要获取参数值

function getPara(href, name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = href.match(reg); //获取url中"?"符后的字符串并正则匹配
  var context = "";
  if (r != null)
    context = r[2];
  reg = null;
  r = null;
  return context == null || context == "" || context == "undefined" ? "" : context;
}

href为去掉?的字符串。name为你想获取的参数

 

在页面传值获取参数值时...又会遇到有些中文会经过url的转码,导致的乱码

此时,我使用base64.js进行转码与解码