How to refresh a page with jQuery by passing a par

2019-01-22 17:44发布

I am refreshing my page using jQuery:

location.reload();

This is working great but I want to refresh the same page by passing a parameter to URL.

How can I do this by using jQuery?

Ex:

If my url is www.myweb.com, I want to refresh this by passing a parameter like this

  www.myweb.com?single

Thank you

9条回答
戒情不戒烟
2楼-- · 2019-01-22 18:08

I would use REGEX with .replace like this:

window.location.href = window.location.href.replace( /[\?#].*|$/, "?single" );
查看更多
一夜七次
3楼-- · 2019-01-22 18:11

You should be able to accomplish this by using location.href

if(window.location.hostname == "www.myweb.com"){
   window.location.href = window.location.href + "?single";
}
查看更多
在下西门庆
4楼-- · 2019-01-22 18:13

You can use Javascript URLSearchParams.

var url = new URL(window.location.href);
url.searchParams.set('single','');
window.location.href = url.href;
查看更多
登录 后发表回答