简体   繁体   English

检测URL更改是否返回

[英]Detect if URL change was back

I have a web app that uses some subtle effects when navigating. 我有一个网络应用程序,在导航时会使用一些微妙的效果。 Essentially the pages "slide" in from the right. 本质上是页面从右侧“滑入”。

Everything in the app is based changes to the hash and all that is working great. 应用程序中的所有内容都是基于对哈希的更改,并且一切正常。

What I'm simply looking for is a way to determine if the change to the URL is a "back" action. 我只是在寻找一种确定URL更改是否为“后退”操作的方法。 I'm not going to try to affect the change-- my app already handles that perfectly. 我不会尝试影响更改-我的应用程序已经可以完美地处理该更改。 I just want to reverse the "effect". 我只想扭转“影响”。 In other words, if I know they are clicking "back" I want to slide in from the left left instead of the usual action of sliding in from the right. 换句话说,如果我知道他们单击“后退”,我想从左向左滑动,而不是从右向右滑动。

Keep in mind I don't need help with the animation itself or keeping track of app state in the hash-- I only need to know how to know if the change is a "Back" action. 请记住,我不需要动画本身的帮助,也不需要跟踪哈希中的应用程序状态-我只需要知道如何知道更改是否为“后退”操作。

Also, I really don't want to keep a list of the hash states in the app as users click around to try and determine if they are going "back". 另外,我真的不想在应用程序中保留哈希状态列表,因为用户单击来尝试确定他们是否要“返回”。 I might consider that if it's really the only option, but I hoping to find a solution that just knows if the user initiated back (Back button, history.go(-1), right click "Go Back", etc). 我可能会认为,如果它确实是唯一的选择,但我希望找到一个解决方案,该解决方案仅知道用户是否已发起返回(“返回”按钮,history.go(-1),右键单击“返回”,等等)。

Is this information available in javascript without explicitly keeping track of changes to the hash? 在没有明确跟踪哈希值更改的情况下,是否可以在javascript中获得此信息?

Use a previous page variable. 使用上一页变量。 I'm going to assume that when you are "changing" pages you are doing it through a central function. 我将假设当您“更改”页面时,您是通过中央功能来完成此操作的。

In your goto page function 在转到页面功能中

// Try to get the previous page if known (will be 'unidentified' if unknown)
var prevPage = window.previousPage;

// Set this page as the previous before moving.
window.previousPage = 'this page';

// Assume that gotoPage is where we are headed
if(gotoPage == prevPage)
{
    // We're going back!
}
else
{
    // We're going forward!
}

PS 聚苯乙烯

I am using the window.previousPage as the general container and using prevPage to compare in scope. 我使用window.previousPage作为常规容器,并使用prevPage在范围内进行比较。 This is on by design to limit the number of places you would have to set window.previousPage 这是设计使然,以限制必须设置window.previousPage的位置数。

EDIT: 编辑:

Didn't think about multiple going backs, same idea using an array (not tested but this is the general idea) 没考虑过多次回溯,使用数组的想法相同(未经测试,但这是一般想法)

var prevPage = '';

if( Object.prototype.toString.call( window.previousPage ) === '[object Array]' )
    prevPage = window.previousPage[ window.previousPage.length - 1 ];

if(prevPage == gotoPage)
{
    // Splice out the last entry
    window.previousPage.splice( window.previousPage.length - 1, 1);
    // Going back DO NOT add new window.previousPage
}
else
{
    // Going forward add the previousPage before changing
    window.previousPage[ window.previousPage.length ] = 'this page';

    // Move Page
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM