zoukankan      html  css  js  c++  java
  • window.location对象 获取页面地址

    window.location对象的属性:

    属性 含义
    location.protocol 协议 "http://"或"https://"
    location.hostname 服务器名字 "baidu.com"
    location.port 端口 "8080"
    location.pathname URL中主机名后的部分 "/index.php"
    location.search "?"后的部分,又称查询字符串 "?type=2&id=122"
    location.hash "#"之后的内容 "#first"
    location.host 等于hostname+port "baidu.com:8080"
    location.href 当前页面的完整URL "http://baidu.com:8080?type=2&id=50#first"

    window.location对象的方法:

    方法 描述
    location.assign() 加载新的文档
    location.reload() 重新加载当前文档
    location.replace() 用新的文档替换当前文档

    location.href 与location.assign()以及location.replace()的区别:

    location.href='http://baidu.com'    ===  location.assign('http://baidu.com')   都是在当前页面跳转到新的页面,在新页面点击返回按钮,可回到上一页。

    location.replace('http://baidu.com') 当前页面被新页面替换,不能回到上一页。

    如何使用js脚本捕获页面GET方法请求的参数?    比如 "?type=2&id=50" 里的2和50。

    1 var dataList=window.location.search;  //使用location.search获取?type=2&id=50字符串。
    2 var dataArray=dataList.split("&");    //用"&"将字符串进行分割,返回到数组中。得到{?type=2,id=50}
    3 var type=dataArray[0].split("=")[1];  //用"="将数组中的"?type=2"分割为数组{?type,2},并取index=1的字符串2。
    4 var id=dataArray[1].split("=")[1];    //同上
    5 console.log(type,id); //2  50

    补充:字符串的split()方法

    split(separator,howmany);   

    separator:必需,字符串或正则表达式。从该参数指定地方分割。被分割的字符返回到数组中,且不包含separator本身。

    howmany:可选,指定返回数组的最大长度。

  • 相关阅读:
    [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
    [Angular] Lazy Load CSS at runtime with the Angular CLI
    [置顶] 半年的工作总结
    分享4个未注册*sdn域名
    Windows 已在 DImageProcess.exe 中触发一个断点。
    Foundation: Binary Search
    HDU 3016 Man Down (线段树+dp)
    网络智能和大数据公开课Homework3 Map-Reduce编程
    centos 6.2 关闭 IPV6
    [置顶] 【Git入门之八】分支管理
  • 原文地址:https://www.cnblogs.com/dreamperson/p/10724461.html
Copyright © 2011-2022 走看看