use URL object insten of String operations

thanks to davydov-vyacheslav
This commit is contained in:
6543 2019-08-28 02:34:37 +02:00
parent 28eb5848ef
commit ce51b467c4
Signed by: 6543
GPG Key ID: A1CA74D27FD13271

View File

@ -22,50 +22,24 @@ window.sortt=function(normsort,revsort,isdefault){
NO: if sort=“” indikates default (with * prefix) set url_sort= NO: if sort=“” indikates default (with * prefix) set url_sort=
YES: set url_sort variable YES: set url_sort variable
*/ */
url_sort=""; let url = new URL(window.location);
if (window.location.search.indexOf("sort=") < 0) { let url_sort = url.searchParams.get("sort");
if ( isdefault ) url_sort = normsort; if ((url_sort === null) && isdefault) url_sort = normsort;
url_sort = "";
} else {
url_sort=window.location.search.substr(window.location.search.indexOf("sort=")+5);
if (url_sort.indexOf("&") >= 0) url_sort=url_sort.substr(0,url_sort.indexOf("&"));
}


// generate new URL // generate new URL
/* script check if url_sort and sort attribute is same /* script check if url_sort and sort attribute is same
YES: check if reverse attribute exist -> YES: generate URL with reverse sort param and open NO: generate URL with sort param
NO: generate URL with sort param and open YES: check if reverse attribute exist -> YES: generate URL with reverse sort param
*/ */
url_new = window.location.protocol + "//" + window.location.host + window.location.pathname;
if (window.location.search.length == 0) {
if (url_sort != normsort) { if (url_sort != normsort) {
window.location.replace(url_new + "?sort=" + normsort); url.searchParams.delete("sort");
url.searchParams.append("sort",normsort);
} else if (revsort != "") { } else if (revsort != "") {
window.location.replace(url_new + "?sort=" + revsort); url.searchParams.delete("sort");
} url.searchParams.append("sort",revsort);
} else {
if (window.location.search.indexOf("sort=") >= 0) {
tmp = window.location.search.substr(window.location.search.indexOf("sort=")+5);
if (tmp.indexOf("&") >= 0) {
tmp = tmp.substr(tmp.indexOf("&"));
} else {
tmp = "";
}
}

if (window.location.search.indexOf("sort=") >= 0) {
if (url_sort != normsort) {
window.location.replace(url_new + window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + normsort + tmp);
} else if (revsort != "") {
window.location.replace(url_new + window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + revsort + tmp);
}
} else {
if (url_sort != normsort) {
window.location.replace(url_new + window.location.search + "&sort=" + normsort);
} else if (revsort != "") {
window.location.replace(url_new + window.location.search + "&sort=" + revsort);
}
}
} }


//open url
window.location.replace(url.href);
}; };