使用Javascript:如果没有选项卡或窗口的历史logging,如何创build一个将用户转到链接的“返回”链接?

编辑-2:没有任何答案似乎工作。 甚至没有我以前标记为这个问题的答案。 任何帮助表示赞赏。 谢谢。

首先,我已经search了一个如何创build一个允许用户返回到前一页的“返回”链接,这是两种方式:

<a href="javascript:history.go(-1)">[Go Back]</a> 

和…

 <a href="#" onclick="history.go(-1);return false;">[Go Back]</a> 

哪两个是更好的select? 为什么? (另外,请注意浏览器兼容性。)

这是问题的一半。 现在,如果我的用户访问的是第一页,那么“返回”链接将不起作用,对吧? (因为窗口或标签没有预先存在的历史logging)。在这种情况下,我想要链接回退并将用户带到http://example.com 。

即如果历史存在,则用户被带到上一页,如果没有,则被带到http://example.com 。

我怎么做? 希望有人能帮忙。

编辑:请注意,我不知道JavaScript,所以请你的答案解释。 谢谢。

您无法检查window.history.length因为它包含您在给定会话中总共访问的页面数量:

window.history.length (整数)

只读。 返回会话历史logging中元素的数量,包括当前加载的页面。 例如,对于在新选项卡中加载的页面,此属性返回1. 引用1

比方说,用户访问您的网页,点击一些链接,并返回:

 www.mysite.com/index.html < - 第一页,现在当前页<---- +
 www.mysite.com/about.html |
 www.mysite.com/about.html#privacy | 
 www.mysite.com/terms.html < - 用户使用后退button或您提供的解决scheme返回

现在window.history.length是4.由于安全原因,你不能遍历历史项目。 否则可能会读取用户的历史logging,并获得他的网上银行会话ID或其他敏感信息。

您可以设置一个超时,这将使您能够采取行动,如果前一页没有加载在给定的时间。 但是,如果用户的Internet连接速度较慢,并且超时时间很短,则此方法将始终将其redirect到默认位置:

 window.goBack = function (e){ var defaultLocation = "http://www.mysite.com"; var oldHash = window.location.hash; history.back(); // Try to go back var newHash = window.location.hash; /* If the previous page hasn't been loaded in a given time (in this case * 1000ms) the user is redirected to the default location given above. * This enables you to redirect the user to another page. * * However, you should check whether there was a referrer to the current * site. This is a good indicator for a previous entry in the history * session. * * Also you should check whether the old location differs only in the hash, * eg /index.html#top --> /index.html# shouldn't redirect to the default * location. */ if( newHash === oldHash && (typeof(document.referrer) !== "string" || document.referrer === "") ){ window.setTimeout(function(){ // redirect to default location window.location.href = defaultLocation; },1000); // set timeout in ms } if(e){ if(e.preventDefault) e.preventDefault(); if(e.preventPropagation) e.preventPropagation(); } return false; // stop event propagation and browser default event } 
 <span class="goback" onclick="goBack();">Go back!</span> 

请注意, typeof(document.referrer) !== "string"非常重要,因为浏览器供应商可能因安全原因(会话散列,自定义GET URL)而禁用引用。 但是如果我们检测到一个引用者,并且它是空的,那么可以说没有以前的页面(见下面的注释)。 仍然可能会出现一些奇怪的浏览器怪癖,所以使用超时比使用简单的redirect更安全。

编辑:不要使用<a href='#'>...</a> ,因为这将添加到会话历史的另一个条目。 最好使用<span>或其他元素。 请注意,如果您的页面位于(i)框架内,则typeof document.referrer始终为"string"而不是空的。

也可以看看:

  • W3C:HTML5:5.4.2历史界面

检查window.history.length或简单地, history.length

编辑: 一些浏览器开始他们的历史0,其他与1。

如果它的值为1,这意味着它是该窗口/选项卡的第一页 – 那么你可以让JSredirect你。

 <script> function backAway(){ //if it was the first page if(history.length === 1){ window.location = "http://www.mysite.com/" } else { history.back(); } } </script> <a href="#" onClick="backAway()">Back</a> 

两者都是一样的,只是两种不同的方法来调用同一个函数。 尝试以下操作:

 <a href="javascript:history.back();">[Go Back]</a> 

使用return:false;的原因return:false; 在这个问题上得到了很好的解释。

对于另一个问题,您可以检查引用者是否为空:

  function backAway(){ if (document.referrer == "") { //alternatively, window.history.length == 0 window.location = "http://www.example.com"; } else { history.back(); } } <a href="#" onClick="backAway()">Back Button Here.</a> 
 echo "<p><a href=\"javascript:history.go(-1)\" title=\"Return to previous page\">&laquo;Go back</a></p>"; 

会回去一页。

 echo "<p><a href=\"javascript:history.go(-2)\" title=\"Return to previous page\">&laquo;Go back</a></p>"; 

将返回两页。

这似乎是诀窍:

 function goBackOrGoToUrl() { window.history.back(); window.location = "http://example.com"; } 

呼叫history.back(),然后更改位置。 如果浏览器能够回到历史中,将无法进入下一个语句。 如果无法返回,则会转到指定的位置。

添加了一个新的答案来显示格式化的代码:

问题是你正在检查document.referer,因为你是在返回总是正确的,然后导航到http://mysite.com 。 尝试以下操作:

 function backAway(){ if (document.referrer) { //firefox, chrome, etc.. i = 0; } else { // under ie i = 1; } if (history.length>i) { // there are items in history property history.back(); } else { window.location = 'http://www.mysite.com/'; } return false; } 

只需使用cookie来存储您访问过的页面列表。

并应用一些其他的。

编辑:也使用ServerVariables HTTP_REFERER。

你需要检查document.referrerhistory.length就像我在这里回答类似的问题: https : //stackoverflow.com/a/36645802/1145274

下面的代码为我做了诡计。

HTML:

 <div class="back" onclick="goBackOrGoHome()"> Back </div> 

JS:

 home_url = [YOUR BASE URL]; pathArray = document.referrer.split( '/' ); protocol = pathArray[0]; host = pathArray[2]; url_before = protocol + '//' + host; url_now = window.location.protocol + "//" + window.location.host; function goBackOrGoHome(){ if ( url_before == url_now) { window.history.back(); }else{ window.location = home_url; }; } 

所以,你使用document.referrer来设置你来自的页面的域名。 然后你用window.location比较你的当前url。

如果它们来自同一个域,则表示您来自您自己的站点,并将它们发送给window.history.back() 。 如果他们不一样,你从别的地方来,你应该重新定向回家或做任何你喜欢的事情。

如何使用历史replacefunction将网站添加到浏览器的历史?

一旦窗口加载,尝试执行以下操作 –

 history.replaceState("example", "title", "http://www.example.com"); 

即使这是他们访问的第一个页面,也希望这样做,在代码中定义的URL将是它们在返回时被采用的内容。