如何在不刷新的情况下删除URL部分或哈希?

时间:2013-05-27 07:47:24

标签: javascript jquery url jquery-mobile

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

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

我可以通过,但最终的网址现在看起来example.com/contacts#Contact

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

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

如何从#Contcat删除contactsexample.com/contacts#Contact

1 个答案:

答案 0 :(得分: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文件。