-1

I've a jQuery mobile multi-page template structure.

I need to redirect the example.com/contacts page requests to #Contact page

I'm able to do this via but the final URL now looks example.com/contacts#Contact

I need either example.com/contacts or example.com/#Contact

If I tried to remove the hash by setting window.location.hash=""; it will be redirected to default Home page.

How can I remove the #Contcat or contacts from example.com/contacts#Contact?

4

1 回答 1

2

解决方案 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 文件。

于 2013-05-27T08:20:59.063 回答