【问题标题】:How to remove URL part or hash without refreshing?如何在不刷新的情况下删除 URL 部分或哈希?
【发布时间】:2013-05-27 07:47:24
【问题描述】:

我有一个 jQuery 移动多页模板结构。

我需要将example.com/contacts 页面请求重定向到#Contact 页面

我可以通过以下方式执行此操作,但最终 URL 现在看起来是 example.com/contacts#Contact

我需要example.com/contactsexample.com/#Contact

如果我尝试通过设置 window.location.hash=""; 来删除哈希,它将被重定向到默认主页。

如何从example.com/contacts#Contact 中删除#Contcatcontacts

【问题讨论】:

标签: javascript jquery url jquery-mobile


【解决方案1】:

解决方案 1

如果您像这样处理页面更改,则可以轻松完成:

$.mobile.changePage("#second", {transition: "slide",reverse: true,changeHash: true});  

基本上你希望将 changeHash 设置为 false。

工作示例:

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>    
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    <script>
        $(document).on('pagebeforeshow', '#index', function(){       
            $(document).on('click', '#change-page', function(){   
                $.mobile.changePage("#second", {transition: "slide",reverse: true,changeHash: false});        
            });    
        }); 
    </script>
</head>
<body>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
            <a href="#second" class="ui-btn-right">Next</a>
        </div>

        <div data-role="content">
            <div data-role="button" id="change-page">Change Page</div>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div> 
    <div data-role="page" id="second">
        <div data-theme="a" data-role="header">
            <h3>
                Second Page
            </h3>
            <a href="#index" class="ui-btn-left">Back</a>
        </div>

        <div data-role="content">

        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>    
</body>
</html>  

解决方案 2

如果您不想以编程方式处理它,您可以对您的 jQuery Mobile js 文件进行轻微更改。首先下载未压缩的 jQM js 文件并打开它。我说的是当前版本 1.3.1)。

查找第 4730 行,但由于此代码每天都在变化,如果它不在 taht 行中,则查找此代码段:

$.mobile.changePage.defaults = {
    transition: undefined,
    reverse: false,
    changeHash: true,
    fromHashChange: false,
    role: undefined, // By default we rely on the role defined by the @data-role attribute.
    duplicateCachedPage: undefined,
    pageContainer: undefined,
    showLoadMsg: true, //loading message shows by default when pages are being fetched during changePage
    dataUrl: undefined,
    fromPage: undefined,
    allowSamePageTransition: false
};

改成:

$.mobile.changePage.defaults = {
    transition: undefined,
    reverse: false,
    changeHash: false,
    fromHashChange: false,
    role: undefined, // By default we rely on the role defined by the @data-role attribute.
    duplicateCachedPage: undefined,
    pageContainer: undefined,
    showLoadMsg: true, //loading message shows by default when pages are being fetched during changePage
    dataUrl: undefined,
    fromPage: undefined,
    allowSamePageTransition: false
};

注意,区别在于:

changeHash: false,

当你这样做时,找到一些在线工具并压缩这个js文件。

【讨论】:

  • 我知道changeHash: true 的事情,问题是我已经在#Contcat 页面中,我只想默默地删除哈希
  • 那么重点是什么,这将删除哈希?看看我的其他解决方案。我建议你不要手动更改它,你可能会导致更多问题。
猜你喜欢
  • 2010-11-26
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
相关资源
最近更新 更多