向URL添加参数 - 始终确保它放在其他参数之前

时间:2022-08-24 12:12:55

I have a script that I use to switch between two layout when a button is clicked by adding a string to my url. It works great - but I need to control how the param string is inserted to my url to work with a filter that also adds strings to the url.

我有一个脚本,当我通过向我的网址添加一个字符串来点击一个按钮时,我用它来切换两个布局。它工作得很好 - 但我需要控制如何将param字符串插入到我的url中以使用过滤器,该过滤器还会向url添加字符串。

The url structure looks like this

网址结构如下所示

/Computers/Desktops = loads gridcode
first click = /Computers/Desktops?param=list = loads listview
second click = /Computers/Desktops?param=grid = loads gridview

This works great - but my problem is that I have a set of filters that adds strings to the url which mess my layout switch not working.

这很好用 - 但我的问题是我有一组过滤器,它们会在url中添加字符串,这会让我的布局开关无法正常工作。

Example:

例:

first click = /Computers/Desktops?param=list = loads listview

I start to add filters:

我开始添加过滤器:

/Computers/Desktops?param=list&fss=2GB+SSD

This works - but when I change the layout:

这有效 - 但是当我改变布局时:

/Computers/Desktops&fss=2GB+SSD?param=grid

Here the paramstring is inserted after the filterstring, and this breaks the filtering. I need it to be inserted before:

这里的paramstring是在filterstring之后插入的,这会破坏过滤。我之前需要插入它:

 /Computers/Desktops?param=grid&fss=2GB+SSD

There is also a variable where someone could filter before switching a layout:

还有一个变量,有人可以在切换布局之前进行过滤:

/Computers/Desktops?fss=HP+2GB

And when switching layout now:

现在切换布局时:

/Computers/Desktops?fss=HP+2GB?param=list

Also here the param needs to be inserted before the filterstrings. The /Computers/Desktops is dynamic and changes as well depending on category.

此外,param需要在过滤器字符串之前插入。 / Computers / Desktops是动态的,并且根据类别也会发生变化。

My Script

我的剧本

$(function() {
    if (window.location.href.indexOf("param=list") > -1) {
        //listcode
    } else {
        //gridcode
    }
    $('.click').on('click', function() {
        console.log("Clicked");
        var url = window.location.pathname;
        var url = window.location.href;
        if (url.indexOf('?param=list') > -1) {
            url = url.replace("?param=list", "") + '?param=grid'
        } else {
            url = url.replace("?param=grid", "") + '?param=list'
        }
        window.location.href = url;
    });
});

1 个解决方案

#1


1  

You can do it this way by using the above mentioned function:

您可以通过使用上述功能这样做:

 $('.click').on('click', function() {
var baseUrl =  window.location.href.split("?")[0];
var fss = getParametersByName("fss");
var params = getParametersByName("params");
if(params == "list") params = "grid"; else params = "list";
var newUrl = baseUrl+"?params="+params;
if((fss).length>0) newUrl = newUrl+"&fss="+fss;
window.location.href=newUrl;

}

}

function getParametersByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));}

from here:How can I get query string values in JavaScript? which is already mentioned.

从这里:如何在JavaScript中获取查询字符串值?这已经提到了。

#1


1  

You can do it this way by using the above mentioned function:

您可以通过使用上述功能这样做:

 $('.click').on('click', function() {
var baseUrl =  window.location.href.split("?")[0];
var fss = getParametersByName("fss");
var params = getParametersByName("params");
if(params == "list") params = "grid"; else params = "list";
var newUrl = baseUrl+"?params="+params;
if((fss).length>0) newUrl = newUrl+"&fss="+fss;
window.location.href=newUrl;

}

}

function getParametersByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));}

from here:How can I get query string values in JavaScript? which is already mentioned.

从这里:如何在JavaScript中获取查询字符串值?这已经提到了。