一、简介

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

二、实例

1.hash

#百度统计
window. _hmt.push(['_trackPageview', "/" + window.location.hash])

2.host

#如果端口是 80,那么就没有端口
console.log(window.location.host)

3.hostname

#只有ip,没有端口
console.log(window.location.hostname)

4.href

document.getElementById("demo").innerHTML = "页面位置是 " + window.location.href;

5.pathname

#端口/之后的全路径,如/login?name=maple&pwd=123
document.getElementById("demo").innerHTML = "页面路径是 " + window.location.pathname;

6.port

document.getElementById("demo").innerHTML = "页面端口是 " + window.location.port;

7.protocol

#一般都是http或者是https
document.getElementById("demo").innerHTML = "页面协议是 " + window.location.protocol;

8.search

#将url中?中的参数遍历出来,location.search.substring(1)的值就是 "name=maple&pwd=123"
function getQueryByKey(key) {
let query = window.location.search.substring(1);
let vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split("=");
if (pair[0] === key) {
return pair[1];
}
}
return (false);
}

9.assign

<html>
<head>
<script>
function newDoc() {
window.location.assign("https://www.baidu.com")
}
</script>
</head>
<body> <input type="button" value="Load new document" onclick="newDoc()"> </body>
</html>

window.location 对象中各种方法的用途的更多相关文章

  1. JavaScript window.location对象

    JavaScript window.location对象   示例 注意 方法 经常使用window.location,它的结构总是记不住,简单梳理下,方便以后查询. 示例 URL:http://b. ...

  2. 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具

    查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...

  3. Window Location对象

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. window.location 对象在编写时可不使用 window 这个前缀. Location ...

  4. 2016/2/22 1、DOM的基本概念 2、Window对象操作 3、Windows.history对象 4、Window.location对象 5、Window.status对象

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  5. window.location对象 获取页面地址

    window.location对象的属性: 属性 含义 值 location.protocol 协议 "http://"或"https://" location ...

  6. window.location 对象所包含的属性

    window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostname 当前 URL 的主机名 hre ...

  7. window.location.href的使用方法

    http://hljqfl.blog.163.com/blog/static/40931580201122210573364/ 在写ASP.Net程序的时候,我们常常遇到跳转页面的问题,我们常常使用R ...

  8. request,reponse对象中的方法

    1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1   obj ...

  9. javascript Location对象属性和方法

    Location对象 Location对象包含有关当前URL的信息 https://blog.csdn.net/u010953692/article/details/80901035#3 https( ...

随机推荐

  1. linux fcntl 对文件描述符控制

    linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制

  2. final和abstract关键字的作用

    final和abstract关键字的作用 final和abstract是功能相反的两个关键字,可以对比记忆 abstract可以用来修饰类和方法,不能用来修饰属性和构造方法:使用abstract修饰的 ...

  3. 第二天:PowerShell别名

    1.查询别名: Get-Alias -name ls Get-Alias -name dir Get-Alias -name fl Get-Alias -name ft 2.查看可用的别名 查看可用的 ...

  4. MyBatis中大于和小于号的转义写法

    mybatis 中 SQL 写在mapper.xml文件中,而xml解析 < .>.<=.>= 时会出错,这时应该使用转义写法. 方式一 < <= > > ...

  5. uboot刷机

    ath> tftp xx.zpk Trying eth0 Using eth0 device TFTP from server 192.168.1.104; our IP address is ...

  6. 使用node搭建服务时,服务可以启动,但是无法访问

    一开始搭建的是没有问题,能够正常启动和访问,然后吃了个饭回来就变成只能启动,浏览器访问不了了. 说真的,这是一个常识问题.使用node搭建服务访问本地文件,服务搭建的没有问题,能够正常启动.但是在浏览 ...

  7. c++ GetAsyncState() 函数

    函数原型 SHORT GetAsyncKeyState(int vKey); 例:若判断 回车键 if(GetAsyncKeyState(VK_RETURN)&0x8000) ( return ...

  8. JAVA错误:Exception in thread "main" java.lang.NullPointerException

    JAVA错误:Exception in thread "main" java.lang.NullPointerException例如: Exception in thread &q ...

  9. 「题解」:Kill

    问题 A: Kill 时间限制: 1 Sec  内存限制: 256 MB 题面 题面谢绝公开. 题解 80%算法 赛时并没有想到正解,而是选择了另一种正确性较对的贪心验证. 对于每一个怪,我们定义它的 ...

  10. GDI+图像与GDI位图的相互转换

    Delphi的TBitmap封装了Windows的GDI位图,因此,TBitmap只支持bmp格式的图像,但是在Delphi应用程序中,常常会遇到图形格式的转换,如将Delphi位图TBitmap的图 ...