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

Javascript 如何在不刷新的情况下删除URL部分或哈希?,javascript,jquery,url,jquery-mobile,Javascript,Jquery,Url,Jquery Mobile,我有一个jQuery移动多页模板结构 我需要将example.com/contacts页面请求重定向到#Contact页面 我可以通过,但最终的URL现在看起来是example.com/contacts\Contact 我需要example.com/contacts或example.com/#Contact 如果我试图通过设置window.location.hash=“”)来删除哈希它将被重定向到默认主页 如何从example.com/contacts\Contact中删除#Contcat或联系

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

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

我可以通过,但最终的URL现在看起来是
example.com/contacts\Contact

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

如果我试图通过设置
window.location.hash=“”)来删除哈希它将被重定向到默认主页

如何从
example.com/contacts\Contact
中删除
#Contcat
联系人

解决方案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>  
将其更改为:

$.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文件。

你可能正在寻找。它只适用于较新的浏览器——较旧的浏览器将不得不使用hashtag,并且您不能更改URL的任何部分,除了hashtag(没有HTML5),而不重新加载页面(安全原因)。是一个很好的跨浏览器解决方案。我认为这是不可能的。您可以在此[讨论][1]中找到更多详细信息。[1] :签出SO和附件。您可以使用
$.mobile.changePage('url',{changeHash:false})进行签出。URL或#pageID。我知道
changeHash:true
这件事,问题是我已经在
#Contcat
页面中,我只想默默地删除散列。那么,这有什么意义呢?看看我的另一个解决方案。我建议您不要手动更改它,您可能会导致更多问题。
changeHash: false,