html如何隐藏url的参数,【已解决】html和js中如何设置或清空url中的查询参数

现在需要在点击查询按钮,清空,最好可以独立设置url中的查询参数:

4ba22d78e623c704aa88c9e690dfeb0e.png

index.html?q=&tag=Individuality&difficulty=7

变成:

index.html?q=

或:

index.html

js clear html url query string

js clean html url query string

Manipulating the browser history – Web APIs | MDNvar locationHref = window.location.href

console.log("locationHref=%o", locationHref)

调试输出:locationHref="file:///Users/crifan/dev/dev_root/company/xxx/projects/xxx/www/xxxWeb/index.html?q=&tag=Individuality&difficulty=7"

另外:window.history.pushState({"q": ""}, document.title, "/" + HomePageUrl)

输出:main.js:18 Uncaught DOMException: Failed to execute 'pushState' on 'History': A history state object with URL 'file:///index.html' cannot be created in a document with origin 'null' and URL 'file:///Users/crifan/dev/dev_root/company/xxx/projects/xxx/www/xxxWeb/index.html?q=&tag=Individuality&difficulty=7'.

【总结】

最后用代码:// from: file:///xxx/xxxWeb/index.html?q=&tag=Individuality&difficulty=7

// got: /xxx/xxxWeb/index.html

function getCurUrlPath(){

// var curUrl = window.location.href.substring(window.location.href.lastIndexOf('/') + 1).split("?")[0]

// console.log("curUrl=%o", curUrl)

// locationHref="file:///xxx/xxxWeb/index.html?q=&tag=Individuality&difficulty=7"

var locationPathname = window.location.pathname

console.log("locationPathname=%o", locationPathname)

return locationPathname

}

// clear query string in url: index.html?q=&tag=Individuality&difficulty=7

// to : index.html

function clearQueryStrInUrl(){

// var locationHref = window.location.href

// console.log("locationHref=%s", locationHref)

var curUrl = getCurUrlPath()

console.log("curUrl=%s", curUrl)

var curTitle = document.title

console.log("curTitle=%s", curTitle)

// window.history.pushState({"q": ""}, curTitle, curUrl)

window.history.pushState({}, curTitle, curUrl)

}

即可实现:清除当前页面的url中的查询参数。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值