History.go("partial URL")

History.go("partial URL")

我希望我的按钮返回到最后一个 URL,里面有 /furniture/。

在 w3schools 文档中说:

The parameter can either be a number which goes to the URL within the specific position (-1 goes back one page, 1 goes forward one page), or a string. The string must be a partial or full URL, and the function will go to the first URL that matches the string.

https://www.w3schools.com/jsref/met_his_go.asp

因为它是 Squarespace,所以我必须使用事件侦听器将自定义功能添加到 Squarespace 按钮。

<script>
  window.onload=function(){
    document.getElementsByClassName("sqs-block-button-element").addEventListener("click", goBack);

    function goBack() {
    window.history.go("https://uk5-shop.com/furniture/");
    }
  }
</script>

据我所知,这应该会让我回到我上次访问的家具类别中的任何产品,例如 https://uk5-shop.com/furniture/chair or https://uk5-shop.com/furniture/bed。事实并非如此。

我在某处读到这可能会停产,但为什么在大多数文档中都有它?

1.说明:

如另一个问题中的回答:()

Supplying a URL as a parameter is a non-standard feature and will not work in all browsers (as well as google chrome). Most browsers accept only a relative number, e.g. 1 or -1.

但是:为什么要使用 history.go(partialUrl)?

  • 情况1:您只想转到用户访问历史记录中的页面,而不关心浏览器"go back"和"go forward"按钮。
  • 情况2:你想模拟"go back"按钮。

情况一,可以使用cookies保存历史记录。

但在情况 2 中,您可以将历史记录保存在 cookie 中并模拟后退按钮,但是如果用户玩前后按钮,我们将无能为力,脚本将不再准确。


2。脚本:

注意:此脚本保存临时 cookie,因此当用户关闭浏览器历史记录时将被清除。如果您想要永久历史记录,请更改 setCookie 函数 (use this link)。

在每个页面的开头(甚至是您要使用的页面 history.go),添加此脚本以将当前 url 保存在临时 cookie 中:

appendToCookie('history', window.location.href);

为了返回到 url 加工部分 url,添加这个(还要确保你在这个页面的开头也添加了 previus 脚本(仅一次)):


//last parameter description (searchFullWebsiteHistoryAndGoForwardNotBackward) :
//1. for usage case 1 described above, set it to true
//2. for usage case 2 described above, set it to false

var wentSuccessfull = goBackToHistoryWithPartialUrl('history','/some/partial/Url',false);
if(!wentSuccessfull) console.log('the pattern doesnt exists in history!');

function goBackToHistoryWithPartialUrl(historyCookieName, partialUrl, searchFullWebsiteHistoryAndGoForwardNotBackward){
  var history = getArrayFromCookie(historyCookieName);
  var historyIndex = null;
  for(var i = history.length - 2 ; i >= 0; i--){
    if( history[i].indexOf(partialUrl) >-1){
      historyIndex = i;
      break;
    }
  }
  if(historyIndex !== null){
    if (searchFullWebsiteHistoryAndGoForwardNotBackward){
      window.location.href = history[historyIndex];
    }
    else{
      var is = [];
      for(var i = history.length - 1 ; i >= historyIndex; i--){
        is.push(i);
      }
      removeMultiFromCookie(historyCookieName,is);
      //waitForCookieToSet();
      window.history.go(historyIndex - (history.length - 1));
    }
    return true;
  }else{
    return false;
  }
}

辅助函数:

function getSeparator() {
  return '_#_SEP_#_';
}

function getArrayFromCookie(cookieName){
  var c = getCookie(cookieName);
  var a = c.split(getSeparator());
  return !c ? [] : a;
}

function appendToCookie(cookieName, value){
  var a = getArrayFromCookie(cookieName);
  a.push(value);
  setCookie(cookieName, a.join(getSeparator()));
}
function removeFromCookie(cookieName, index){
  var arr = getArrayFromCookie(cookieName);
  arr.splice(index,1);
  setCookie(cookieName, arr.join(getSeparator()));
}
function waitForCookieToSet(){
    //
    var fakeAjax = new XMLHttpRequest();
    var anything = fakeAjax.responseText;
    fakeAjax.open("GET","ajax_info.txt",false);  // file doesn't actually exist
    fakeAjax.send();
}
function removeMultiFromCookie(cookieName, indices){
  var arr = getArrayFromCookie(cookieName);
  for(var i = indices.length - 1 ; i>=0 ; i-- )arr.splice(indices[i],1);
  setCookie(cookieName, arr.join(getSeparator()));
}
function setCookie(cname, cvalue) {
  document.cookie = cname + "=" + cvalue + ";path=/";
}

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}