当前位置:网站首页>浏览器中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);
加载新文档
边栏推荐
猜你喜欢

UI遍历的初步尝试

Visual low-code system practice based on design draft identification

Docker interview question 2--get the number of database connections and docker-compose

Shader Graph learns various special effects cases

What should I do if there is no sound after reinstalling the system in win10?

Shader Graph学习各种特效案例

开发IM即时通讯容易吗?需要什么技术

unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla

Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection

ABAP 里文件操作涉及到中文字符集的问题和解决方案
随机推荐
R语言使用coxph函数构建生存分析回归模型,使用forestmodel包的forest_model函数可视化生存回归模型对应的森林图
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
eyb:Redis学习(4)
Web性能测试模型小结
信息化和数字化的核心差异
UI遍历的初步尝试
Mysql数据库 ALTER 基本操作
【Grpc】简介
C语言头文件组织与包含原则
数据建模已死,真的吗?
使用 apxs 构建和安装 Apache 扩展共享对象模块
el-input保留一位小数点
Biotin-Cy2 Conjugate, Biotin-Cy2 Conjugate_Cy2 Biotin Conjugate
Problems and solutions related to Chinese character set in file operations in ABAP
Moonbeam网络维护模式(Maintenance Mode)解读
DHCP——动态主机配置协议
为什么字符串一旦创建就不可以改变?
无js实现弹出层效果
阿里云混合云管理平台多Region架构
Data storage - the C language