Jquery Mobile - 检测页面刷新 - 返回主页

Dan*_*cer 5 jquery jquery-mobile

是否可以在Jquery移动HTML5页面中检测页面刷新?

我正在构建一个测验作为客户端项目的一部分 - 测验在一个html页面中浏览jquery移动div页面..例如,如果用户在index.html#quizpage3并刷新我想检测刷新和将导航返回到index.html.

这可能吗?

干杯保罗

Pat*_*ick 3

这是我正在使用的解决方案。在撰写本文时,tt 似乎工作正常,因此可能会对某人有所帮助。然而,这只是一个快速修复,为了获得更好的功能,最好在服务器端进行。

首先;请注意,此代码位于 JQM 代码之前。(来自 JQM 网站:因为 mobileinit 事件是立即触发的,所以您需要在加载 jQuery Mobile 之前绑定事件处理程序。”)。我们获取当前页面的哈希值,但我们不想刷新某些页面类似主页的页面。

<script type="text/javascript">
        var needRedirect = false;
        $(document).bind("mobileinit", function(){
            var thisPage = location.hash;
            if (thisPage != '' && thisPage != '#homepage' && thisPage != '#page1') {
                needRedirect = true;
            }
        });
    </script>
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在你自己的代码的开头:

if (needRedirect) {
    $.mobile.changePage("#homepage");
    needRedirect = false;
}
Run Code Online (Sandbox Code Playgroud)