Javascript 第页没有';t从location.href后退时正确初始化

Javascript 第页没有';t从location.href后退时正确初始化,javascript,back,window.location,initializing,Javascript,Back,Window.location,Initializing,我刚刚完成了大量Javascript的编写(除了最相关的部分外,我将为您提供一切),只剩下一个问题 有一次我需要使用location.href进入文章页面。如果用户从文章页面单击浏览器的后退按钮,他们将返回主页(良好),但页面仍处于最初离开时的状态(不良) Javascript根本没有意识到页面已经改变,也许更令人惊讶的是,HTML也没有改变(有几个地方我用Javascript修改了HTML) 少量披露:这是一个移动网站。我正在收听使用触摸事件短按一个元素(而不是长时间拖动)。我尝试过的任何一种

我刚刚完成了大量Javascript的编写(除了最相关的部分外,我将为您提供一切),只剩下一个问题

有一次我需要使用
location.href
进入文章页面。如果用户从文章页面单击浏览器的后退按钮,他们将返回主页(良好),但页面仍处于最初离开时的状态(不良)

Javascript根本没有意识到页面已经改变,也许更令人惊讶的是,HTML也没有改变(有几个地方我用Javascript修改了HTML)

少量披露:这是一个移动网站。我正在收听使用触摸事件短按一个元素(而不是长时间拖动)。我尝试过的任何一种非Javascript链接都会弄乱拖动部分,因为它们都不太关心手指向下移动了多长时间,而且如果在拖动时尝试移动手指,通常会发疯

我想不出任何其他代码在这里会非常有用,但我很乐意发布它

谢谢

-


根据要求,以下是一些相关代码:

function t_end(e)
{
    var touch = e.changedTouches[0]; // Get the information for finger #1

    // we only really care if we've been dragging already,
    // and the finger that's been lifted is the same (first)
    // finger from the initial touch.
    if(dragging && !change_ready && touch.identifier == touchID)

    {
        var spd = (100.0 * touch.pageX / $(document).width() - last_touch[0].x)/(new Date() - drag_time);

        // if the finger has been down for a very short time,
        // and is not moving quickly when removed, this will
        // count as a click, and not a drag.
        if(new Date() - start_time < 50 && spd < .2 && spd > -.2)
        {
            debugText("leaving for page @ " + roll_data[current_story].link);
            location.href = roll_data[current_story].link;
        }else{

            var dir, new_story;

            // at higher speeds we will swap stories.
            // at lower speeds will will just return.
            if(spd > .2 || spd < -.2)
            {
                if(spd < 0)
                {
                    new_story = (current_story > 0 ? current_story - 1 : story_count - 1);
                    dir = "r2l";
                }else{
                    new_story = (current_story < story_count - 1 ? current_story + 1 : 0);
                    dir = "l2r";
                }
            }else{
                new_story = current_story;

                // nx: new x.  The point to which the
                // finger has dragged the current story
                var nx = 100.0 * touch.pageX / $(document).width() - anchor_point.x;
                if(nx < 0)
                {
                    current_story = (current_story > 0 ? current_story - 1 : story_count - 1);
                    dir = "l2r";
                }else{
                    current_story = (current_story < story_count - 1 ? current_story + 1 : 0);
                    dir = "r2l";
                }
            }

            change_ready = true;
            dragging = false;

            toStory(new_story, dir, "no", 100);
        }
    }
}
功能t_端(e)
{
var touch=e.changedtouchs[0];//获取finger#1的信息
//我们只在乎我们是否已经在拖了,
//被举起的手指是一样的(第一个)
//手指从最初的接触。
if(拖动&&!更改&&touch.identifier==touchID)
{
var spd=(100.0*touch.pageX/$(document).width()-最后一次触摸[0].x)/(新日期()-拖动时间);
//如果手指已经放下很短时间,
//并且在移除时移动不快,这将
//计算为单击,而不是拖动。
如果(新日期()-开始时间<50&&spd<.2&&spd>-.2)
{
debugText(“离开页面@”+滚动数据[当前故事].link);
location.href=roll\u data[当前故事]。链接;
}否则{
var dir,新故事;
//以更高的速度,我们将交换故事。
//在较低的速度下,将只返回。
如果(spd>.2 | | spd<-.2)
{
如果(spd<0)
{
新故事=(当前故事>0?当前故事-1:故事计数-1);
dir=“r2l”;
}否则{
新故事=(当前故事<故事计数-1?当前故事+1:0);
dir=“l2r”;
}
}否则{
新故事=当前故事;
//nx:新的x。指向的点
//finger拖拉了当前的故事
var nx=100.0*touch.pageX/$(document).width()-anchor\u point.x;
if(nx<0)
{
当前故事=(当前故事>0?当前故事-1:故事计数-1);
dir=“l2r”;
}否则{
当前故事=(当前故事<故事计数-1?当前故事+1:0);
dir=“r2l”;
}
}
change_ready=true;
拖动=假;
托斯托里(新故事,目录,“否”,100);
}
}
}
  • 这段代码来自一个故事辊,用于移动站点。故事通过一个列表改变,从屏幕的一侧滚动,从另一侧滚动
  • 此功能是触摸端侦听器指向的功能。无论何时从屏幕上取下手指(任何手指),都会触发该手指。可以找到触摸事件的简单分解
  • roll\u data
    是对象的
    数组
    ,具有多个属性,包括
    链接
    (url)
  • toStory()
    是一个函数,它可以滑动到给定要滑动的故事的选定故事、方向(从左到右或从右到左,由
    “l2r”
    “r2l”
    )以确定是否重置当前故事的位置(由
    “是”
    “否”
    )以及设置更改动画的时间。除第一个参数外,所有参数都是可选的
  • debugText()
    是一个简单的函数,如果布尔值(
    debuging
    )为真,它将设置
    .innerHTML
    (或者更确切地说,通过jQuery设置
    .html()

当页面返回到时,问题的最直接迹象是调试文本仍然存在(并设置为最后设置的内容,在本例中为“离开页面@[url]”)。Javascript也突然停止了(需要告诉几个变量,仅仅因为有人点击了页面就没有处理好页面)。

可以检测页面何时离开,然后将其设置为所需状态

<!DOCTYPE html>
<html>
<head>
    <title>unload demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function resetPage() {
            // alert("unload");
            // reset page here
        }
    </script>
</head>
<body onunload="resetPage();">
    hello world
</body>
</html>

卸载演示
函数resetPage(){
//警报(“卸载”);
//在此重置页面
}
你好,世界

可以检测页面何时离开,然后将内容设置为所需状态

<!DOCTYPE html>
<html>
<head>
    <title>unload demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function resetPage() {
            // alert("unload");
            // reset page here
        }
    </script>
</head>
<body onunload="resetPage();">
    hello world
</body>
</html>

卸载演示
函数resetPage(){
//警报(“卸载”);
//在此重置页面
}
你好,世界

您可以改用history.pushState方法,只需首先确保它受支持即可

if (history.pushState)
    history.pushState("", "", url);
else
    window.location = url; 

您可以改用history.pushState方法,只需确保首先支持它

if (history.pushState)
    history.pushState("", "", url);
else
    window.location = url; 

您拥有所有代码,但仍在挣扎。你认为,我们可以在没有任何代码的情况下帮助你吗?我不太确定应该包含什么代码。它非常密集,我相当确定我已经将其缩小到了这个问题(这最多需要一行不同的代码)。但是,如果你想要更多,我很乐意把它包括在内。查看上面的编辑(马上就来)。我已经添加了