Reputation: 33
I want my button to go back to the last URL with /furniture/ in it.
In the w3schools documentation is says:
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
Because it's Squarespace I have to use an event listener to add custom functionality to a Squarespace button.
<script>
window.onload=function(){
document.getElementsByClassName("sqs-block-button-element").addEventListener("click", goBack);
function goBack() {
window.history.go("https://uk5-shop.com/furniture/");
}
}
</script>
As far as I understand it this should send me back to any product in the furniture category I visited last for example https://uk5-shop.com/furniture/chair or https://uk5-shop.com/furniture/bed . This is not the case.
I have read somewhere that this might be discontinued but then why is it in most documentation?
Upvotes: 2
Views: 2142
Reputation: 8253
1. Description :
As answered in Another question: (https://stackoverflow.com/a/6277304/4718434)
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.
BUT: Why you want to use history.go(partialUrl) ?
In case 1, you can use cookies to save history.
But in case 2, you can save history in cookies and simulate back button, but if user play with back and forth buttons, we can't do anything and script will not be accurate anymore.
2. Script:
NOTE: this script save temp cookies, so when user close browser history will clear. if you want permament history, change setCookie function (use this link).
in start of every page (even the page that you want to use history.go), add this script to save current url in a temp cookie:
appendToCookie('history', window.location.href);
for going back to a url that maches partial url, add this (also make sure you added previus script in start of this page as well (just once)):
//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;
}
}
helper functions:
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(){
//https://stackoverflow.com/questions/17583250/javascript-delete-cookie-before-reload-or-redirect
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 "";
}
Upvotes: 2