禁用jQuery Mobile中的深层链接和哈希标签更改 [英] Disable deep linking and hash tag changes in jQuery Mobile

查看:97
本文介绍了禁用jQuery Mobile中的深层链接和哈希标签更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多技巧和建议,可以在jQuery Mobile中进行深层链接,但是我的情况恰恰相反.

我有一个多页的文档,完全独立.我希望该应用程序的入口点成为第一页,并禁用任何指向其他页面的深层链接(编辑:深层链接"是指书签,或者只是可以返回)在文档中.我也不希望应用程序内的导航影响哈希标签.换句话说,如果用户在我的应用程序中,并且按下了浏览器的后退按钮,我希望他们进入进入我的应用程序之前的任何页面,即使他们不在我的首页上.

我试图在所有内部后退和前进导航中将mobile.changePage方法的changeHash选项设置为"false".但是结果是,当他们使用浏览器的后退按钮时,他们会退回两页.此外,这项技术并未像我希望的那样禁用深度链接.

我希望其他人可以在不需要我提供代码示例的情况下提供建议,因为我的代码在其他方面相当复杂.

解决方案

您可以在mobileinit事件上完全禁用changeHash.修改全局默认值应该放在 head 之后,在 jQuery.js 之前,在 jQuery Mobile 之前.

<script src="jquery.js"></script>
<script>
  $(document).on("mobileinit", function(){
   $.mobile.changePage.defaults.changeHash = false;
   $.mobile.hashListeningEnabled = false;
  });
</script>
<script src="jquery.mobile.js"></script>

然后,您需要收听navigate事件上的 back 按钮,并使用window.history.back()引导用户浏览历史记录.

$(window).on("navigate", function (event, data) {
  if (data.state.direction == 'back') {
    window.history.back();
    return false;
  }
});

演示

I have seen many techniques and advice for getting deep linking to work in jQuery Mobile, but my situation requires just the opposite.

I have a multi-page document, completely self-contained. I want the entry point to the app to be the first page, and to disable any deep links (edit: by "deep links" I mean bookmarks , or simply the ability to return) to the other pages in the document. I also do NOT want navigation within the app to affect the hash tag. In other words, if the user is in my app, and they hit their browser's back button, I want them to go to whatever page they were looking at before they entered my app, even if they are not on my first page.

What I have tried is to set the changeHash option on the mobile.changePage method to "false" on all my internal backward and forward navigation. But the result is that when they use their browser's back button, they go TWO pages back. Furthermore, this technique has not disabled deep linking, as I wanted it to do.

I'm hoping that someone else can advise without the necessity of me providing code examples, since my code is otherwise rather complex.

解决方案

You can disable changeHash completely on mobileinit event. Modifying global defaults should be placed in head after jQuery.js and before jQuery Mobile.

<script src="jquery.js"></script>
<script>
  $(document).on("mobileinit", function(){
   $.mobile.changePage.defaults.changeHash = false;
   $.mobile.hashListeningEnabled = false;
  });
</script>
<script src="jquery.mobile.js"></script>

And then, you need to listen to back button on navigate event and take user through history, using window.history.back().

$(window).on("navigate", function (event, data) {
  if (data.state.direction == 'back') {
    window.history.back();
    return false;
  }
});

Demo

这篇关于禁用jQuery Mobile中的深层链接和哈希标签更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆