当前位置:网站首页>url相关知识点
url相关知识点
2022-07-28 12:50:00 【乖女子@@@】
BOM_location
以当前url为例
https://element.eleme.cn/?id=111&name=222#/zh-CN/component/button
作用
获取当前url相关信息!
属性
以下属性除了origin属性(只读)都是可读可写属性!
// 获取完整的url https://element.eleme.cn/?id=111&name=222#/zh-CN/component/button
location.href
// 获取当前url的域名(标准形式) https://element.eleme.cn
location.origin
// 获取当前url的协议名 https:
location.protocol(*/*ˈprəʊtəkɒl*/*)
// 获取当前url的域名 // element.eleme.cn
location.hostname
// 获取当前url的端口号 ''
location.port
// 获取当前url携带的参数, 以“?”开头 ?id=111&name=222
location.search
// 获取当前url的hash值 ,以“#”开头 #/zh-CN/component/button
location.hash
方法
// 重新加载
location.reload()
// 替换当前路径-打开新的页面
location.replace(新url)
// 获取当前完整url
location.toString() // 预location.href功能相同(但是是只读的,不能修改url)
URL构造函数
作用
创建并返回指定url对象
属性
[1]包含location对象的属性;
const url = new URL('https://element.eleme.cn/?id=111&name=222#/zh-CN/component/button')
console.log(url) // URL对象
console.log(url.href) // https://element.eleme.cn/?id=111&name=222#/zh-CN/component/button
console.log(url.origin) // https://element.eleme.cn
console.log(url.protocol)// https:
console.log(url.hostname) // element.eleme.cn
console.log(url.port) //
console.log(url.search)// ?id=111&name=222
console.log(url.hash) // #/zh-CN/component/button
[2]searchParams属性
- 只读属性
- 返回值为 URLSearchParams对象
- 不会直接显示属性
- 通过点语法也获取不到
searchparams对象
无法直接获取其属性
const url = new URL('https://element.eleme.cn/?id=111&id=222&name=222#/zh-CN/component/button') const { searchParams} = url // URLSearchParams {}
可通过方法获取
(1)get方法:搜索参数的第一个值
console.log('结果', searchParams.get('id')) // 111
(2)getAll方法:获取指定搜索参数的所有值,返回是一个数组
console.log(searchParams.getAll('id')) //['111','222'] console.log(searchParams.getAll('b')) // []
(3)has方法:判断是否存在此搜索参数
console.log(searchParams.has('id')) //true console.log(searchParams.has('b')) // false
方法
URL.createObjectURL
语法
URL.createObjectURL(object)object参数可以是一个 File对象 或 Blob对象; 不可是远程链接!
实例
[1]获取url携带参数
(1)通过location对象获取参数
const { search } = location const paramsArr = search.split('?')[1].split('&') const id = paramsArr[0].split('=')[1] // 111 const name = paramsArr[1].split('=')[1] // 222
(2)通过创建URl对象获取参数
const { searchParams } = new URL(window.location) const id = searchParams.get('id') // 111 const name = searchParams.get('name') // 222
边栏推荐
- C语言:顺序存储结构的快速排序
- JWT login authentication + token automatic renewal scheme, well written!
- Beyond istio OSS -- current situation and future of istio Service Grid
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置draw_quantiles参数添加指定分位数横线(例如,50%分位数、中位数)
- 数据库系统原理与应用教程(059)—— MySQL 练习题:操作题 1-10(三)
- Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
- 关于栈的理解以及实际应用场景
- C language: random number + quick sort
- My friend sent me some interview questions
- Customized template in wechat applet
猜你喜欢

After finishing, help autumn move, I wish you call it an offer harvester

30 day question brushing plan (II)

30天刷题训练(一)

Deploy application delivery services in kubernetes (Part 1)

30天刷题计划(三)

SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response

性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...

严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数

Beyond istio OSS -- current situation and future of istio Service Grid

30 day question brushing plan (III)
随机推荐
数据库系统原理与应用教程(060)—— MySQL 练习题:操作题 11-20(四)
DXF读写:对齐尺寸标注文字居中、上方的位置计算
Lyscript get previous and next instructions
Understanding of stack and practical application scenarios
POJ3259虫洞题解
Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
vite在项目中配置路径别名
30天刷题计划(三)
How to play a data mining game entry Edition
算法---不同路径(Kotlin)
How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question
Socket类关于TCP字符流编程的理解学习
微信小程序中自定义模板
Force buckle 2354. Number of high-quality pairs
多线程与高并发(三)—— 源码解析 AQS 原理
C language: quick sorting of sequential storage structure
记一次使用pdfbox解析pdf,获取pdf的关键数据的工具使用
JWT login authentication + token automatic renewal scheme, well written!
C语言:随机生成数+归并排序
Tutorial on the principle and application of database system (060) -- MySQL exercise: operation questions 11-20 (IV)