diff --git a/sortt.js b/sortt.js index f70b310..ba55c6f 100644 --- a/sortt.js +++ b/sortt.js @@ -25,6 +25,7 @@ window.sortt=function(normsort,revsort,isdefault){ url_sort=""; if (window.location.search.indexOf("sort=") < 0) { if ( 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("&")); @@ -38,24 +39,33 @@ window.sortt=function(normsort,revsort,isdefault){ url_new = window.location.protocol + "//" + window.location.host + window.location.pathname; if (window.location.search.length == 0) { if (url_sort != normsort) { - url_new += "?sort=" + normsort + window.location.replace(url_new + "?sort=" + normsort); } else if (revsort != "") { - url_new += "?sort=" + revsort + window.location.replace(url_new + "?sort=" + revsort); } } else { - 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) { + tmp = window.location.search.substr(window.location.search.indexOf("sort=")+5); + if (tmp.indexOf("&") >= 0) { + tmp = tmp.substr(tmp.indexOf("&")); + } else { + tmp = ""; + } } - if (url_sort != normsort) { - url_new += window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + normsort + tmp; - } else if (revsort != "") { - url_new += window.location.search.substr(0,window.location.search.indexOf("sort=")+5) + revsort + 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); + } } } - window.location.replace(url_new); };