当前位置:网站首页>浏览器中location详解
浏览器中location详解
2022-08-10 00:49:00 【dralexsanderl】
浏览器中location详解
window.location
对象用于获取当前页面的URL
信息。
属性解析
1. href
当前页面的URL
。
比如访问github.com
,在控制台中输入location.href
,则结果如下所示
https://github.com/
对href
赋值可以进行URL
跳转:
不携带协议,访问的当前域名对应的资源:
在控制台中输入
location.href = '/asset'
,那么就会跳转到https://github.com/asset
。携带协议的话就跳转到新
URL
:
在控制台中输入location.href = 'http://localhost:8080'
,那么就会跳转到http://localhost:8080
。
其他用法:
top.location.href=”url” 在顶层页面打开url(跳出框架)
self.location.href=”url” 仅在本页面打开url地址
parent.location.href=”url” 在父窗口打开Url地址
在页面中我们可能会嵌套一些iframe
,以上top
,self
,parent
就可以改变iframe
中地址。
2. pathname
当前资源的路径。
比如访问github.com/vuejs
,在控制台中输入location.pathname
,则结果如下所示
/vuejs
对pathname
赋值可以进行访问其他资源,跟href
传入不带协议的用法相同,但是不会处理协议。
在控制台中输入location.pathname = 'http://localhost:8080'
,访问的是github.com/vuejs/http:/localhost/asset
。
3. host
当前页面域名,可能在该串最后带有一个:
并跟上 URL
的端口号。
访问github.com
,在控制台中输入location.host
,结果如下所示:
github.com
访问localhost:8080
,结果如下所示:
localhost:8080
对host
赋值替换掉当前访问的域名,但是后边的资源路径并不会变
当前页面域名https://github.com/vuejs
在控制台中输入location.host = 'localhost:8080'
,当前域名会变成https://localhost:8080/about
。同样不解析协议。
4. hostname
当前页面域名(不包含端口号)
访问github.com
,在控制台中输入location.hostname
,结果如下所示:
github.com
访问localhost:8080
,结果如下所示:
localhost
对hostname
赋值替换掉当前访问的域名,但是后边的资源路径并不会变
当前页面域名https://github.com/vuejs
在控制台中输入location.host = 'localhost:8080'
,当前域名会变成https://localhost:8080/about
。同样不解析协议。
5. port
当前页面域名端口号.
访问github.com
,在控制台中输入location.port
,结果如下所示:
''
访问localhost:8080
,结果如下所示:
8080
对port
赋值替换掉当前访问的域名的端口,其他的并不会变
6. origin
当前页面来源的域名的标准形式。
访问github.com/vuejs
,在控制台中输入location.origin
,结果如下所示:
https://github.com
访问localhost:8080
,结果如下所示:
http://localhost:8080
赋值并不会进行跳转
7. protocol
当前页面域名协议。
访问github.com/vuejs
,在控制台中输入location.protocol
,结果如下所示:
https:
赋值中如果没有协议或者不是标准域名形式都会报错
8. hash
包含页面URL
标识中的 #
和后面 URL
片段标识符
访问github.com
,在控制台中输入location.hash
,结果如下所示:
''
访问localhost:8080/#/about
,结果如下所示:
#/about
通常在hash
路由模式中使用到,而history
模式则无用。
赋值会替换掉#
后的字符
9. search
当前页面URL
中?
字符后面的查询参数。
在hash
和history
模式中,查询参数的获取并不同。
在history
中,通过search
获取。访问https://github.com/search?q=vue&type=
,在控制台中输入location.search
,结果如下所示:
?q=vue&type=
而在hash
中,search
则为空,#
后边的全部字符都在hash
属性中。访问http://localhost:8080/#/about?id=1
,结果:
hash: '#/about?id=1',
search: ''
history
模式中赋值会替换掉?
后的字符,而hash
模式直接插入到#
字符前面
方法
1. reload
location.reload()
用来刷新当前页面,就像刷新按钮一样。
2. replace
location.replace(url);
替换当前页面的URL
,不过当前页面不会保存到会话历史中。这样,用户点击回退按钮时,将不会再跳转到该页面。
3. assign
location.assign(url);
加载新文档
边栏推荐
- Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
- 使用 apxs 构建和安装 Apache 扩展共享对象模块
- oracle的数据导入导出
- eyb:Redis学习(4)
- Sikuli's Automated Testing Technology Based on Pattern Recognition
- @PostConsturct注解作用及特点
- Web性能测试模型小结
- SonarQube升级记录:7.8->7.9->8.9
- Are the numbers entered symmetrical?
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
猜你喜欢
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
防勒索病毒现状分析
365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
02| operator
Mysql数据库 ALTER 基本操作
How to add control panel to right click menu in win7
Visual low-code system practice based on design draft identification
什么是一网统管?终于有人讲明白了
随机推荐
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
基于设计稿识别的可视化低代码系统实践
Summary of Web Performance Testing Models
Data storage - the C language
数据建模已死,真的吗?
跳房子游戏
Summary of basic operations of c language files
Solve the problem of sed replacement text containing special characters such as "/" and "#"
Web性能测试模型小结
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
无js实现弹出层效果
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
【kali-密码攻击】(5.1.2)密码在线破解:Medusa
彩色袜子题
阿里云混合云管理平台多Region架构
UI遍历的初步尝试
R语言使用coxph函数构建生存分析回归模型,使用forestmodel包的forest_model函数可视化生存回归模型对应的森林图
C language structure, function and pointer exercise (simple address book)
color socks problem
【Swoole系列3.5】进程池与进程管理器