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.
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.
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:
/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.
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;
});
});