我希望我的按钮返回到最后一个包含/furniture/的 URL。
在 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或https://uk5-shop.com/furniture/bed 。事实并非如此。
我在某处读到这可能会停止,但为什么大多数文档中都会出现这种情况?
最佳答案
<强>1。描述:
正如另一个问题中的回答:( 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.
但是:为什么要使用history.go(partialUrl)?
- 情况1:您只想转到用户访问历史记录中的页面,并不关心浏览器的“后退”和“前进”按钮。
- 情况2:您想模拟“返回”按钮。
对于情况1,您可以使用cookie来保存历史记录。
但是在情况2中,您可以在cookie中保存历史记录并模拟后退按钮,但是如果用户使用后退按钮,我们将无法执行任何操作,并且脚本将不再准确。
<强>2。脚本:
注意:此脚本保存临时 cookie,因此当用户关闭浏览器历史记录时将会清除。如果您想要永久历史记录,请更改 setCookie 函数( use this link )。
在每个页面的开头(甚至是您要使用history.go的页面),添加此脚本以将当前网址保存在临时cookie中:
appendToCookie('history', window.location.href);
要返回到映射部分 url 的 url,请添加此内容(同时确保您在此页面的开头添加了上一个脚本(仅一次)):
//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(){
//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 "";
}
关于javascript - 历史记录.go ("partial URL"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446415/