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) ?
- Case 1: you just want to go to a page in user visit history, and you don't care about browser "go back" and "go forward" buttons.
- Case 2: you want to simulate "go back" button.
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 "";
}