【问题标题】:Remove hash from URL window.location.hash从 URL window.location.hash 中删除哈希
【发布时间】:2013-03-02 01:03:57
【问题描述】:

我需要从我的 URL 中删除 #,以便完整的 URL 是 example.com/sub-page-title 而不是 example.com/#sub-page-title。

我能够使用 substr(31) 获得一个重复的 URL,31 是 URL 的长度,包括 http://... 但我确信这不是理想的方法。

注意:我无法刷新页面。

点击时,一个外部页面被加载到一个 div 中(然后滑动到位)...一旦可见,我希望 URL 反映这一点(不带 #)。这样前进并点击“返回”会将用户带到实际页面。

代码:

    $(document).ready(function(){

    var hash = window.location.hash.substr(1);
    var href = $('a.load').each(function(){
        var href = $(this).attr('href');
    });


    $('a.load').click(function(){

    var toLoad = $(this).attr('href')+' #project-details';
        $('.wide-frame').animate({
            left: -985,
            duration: 'slow'
        })
    $('#project-details article').hide();   
    $('#project-details').fadeIn('normal',loadContent);


    window.location.hash = $(this).attr('href').substr(31);

    function loadContent() {
        $('#project-details').load(toLoad,showNewContent)
    }
    function showNewContent() { 
        $('#project-details').show('normal');
        $.getScript("<?php bloginfo('template_directory'); ?>/scripts/ajax-control.js");
    }
    return false;

    }); 

});

我不知道这是否相关,但我还在使用带有 jQ​​uery BBQ 的哈希历史来实现其他一些导航功能。

【问题讨论】:

  • 我相信你想做的事情已经在this thread中解决了。

标签: jquery ajax url hash window.location


【解决方案1】:

来个可爱的小RegExp 怎么样?

否则只使用replace() 方法。

像这样:

var string = "url/#hash";
string.replace('#','');

【讨论】:

  • 除非您想将答案扩展为不仅仅是一个链接,并包含代码 sn-ps 或示例,否则这应该是一个注释(最多)。
  • 不知道怎么添加cmets哈哈。真丢人。
  • 是的,对不起...我非常精通 HTML/CSS PHP...但是其他的东西有点超出我的理解,我拼凑了上面的内容,但现在我被卡住了!
  • 我在您更改答案之前做出了回应...我尝试在上面粘贴的代码中的几个位置添加您的解决方案,但没有任何反应。我应该把它放在哪里??
  • 您要从中删除# 的URL 是什么?是 $(this).attr('href').substr(31); ?然后做 $(this).attr('href').substr(31).replace('#','');
猜你喜欢
  • 2015-04-25
  • 2011-05-29
  • 2012-04-19
  • 2012-12-05
  • 2019-07-04
  • 2013-05-15
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多