简体   繁体   English

Javascript 在提交搜索后向 URL 添加排序参数

[英]Javascript adding sort parameter to URL after submitting a search

I would like it to automatically sort from low to high once I click the search button on cvs.com website.我希望它在我点击 cvs.com 网站上的搜索按钮后自动从低到高排序。 Below is what I have so far.以下是我到目前为止所拥有的。 The issue with this is that it keeps refreshing the page.问题在于它不断刷新页面。

How do I stop the page from constantly refreshing after the sort parameter has been set?设置排序参数后,如何阻止页面不断刷新?

if ('URLSearchParams' in window) {
    var searchParams = new URLSearchParams(window.location.search);
    searchParams.set("sort", "pa");
    window.location.search = searchParams.toString();
}

You can use window.stop();您可以使用window.stop();

if ('URLSearchParams' in window) {
    var searchParams = new URLSearchParams(window.location.search);
    searchParams.set("sort", "pa");
    window.location.search = searchParams.toString();
    window.stop();
}

Try this code as it will not refresh the browser:试试这个代码,因为它不会刷新浏览器:

if ('URLSearchParams' in window) {
      const url = new URL(window.location);
      url.searchParams.set('sort', 'pa');
      window.history.pushState({}, '', url);
}

For more details refer this有关更多详细信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM