2013-03-13 172 views
0

我需要从我的URL中删除#,以便完成的URL是example.com/sub-page-title而不是example.com/#sub-page-title。从URL中删除哈希值window.location.hash

我能够得到一个重复的URL去使用substr(31),31是URL的长度,包括http:// ...但我相信这不是一个理想的方式来做到这一点。

注意:我无法刷新页面。

点击后,外部页面被加载到一个div(然后幻灯片到位)...一旦可见,我希望URL反映(没有#)。通过这种方式,点击“返回”将使用户进入实际页面。

CODE:

$(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; 

    }); 

}); 

我不知道这是否是相关的,但我也使用Hash历史与jQuery烧烤一些其他导航功能。

+0

我相信你想做的事已经在[这个线程(http://stackoverflow.com/questions/3338642/updating-address-bar-with-得到解决什么新的网址,而无需哈希 - 或重装浮页)。 – 2013-03-13 21:25:13

回答

-1

可爱的小小RegExp怎么样?

否则只需使用replace()方法。

这样的:

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

这应该是一条评论。 – undefined 2013-03-13 21:07:39

+1

除非你想扩大你的答案不只是一个链接,并包括代码片段或示例,这应该是一个评论(充其量)。 – Matt 2013-03-13 21:08:36

+1

我不知道如何添加评论哈哈。对我感到羞耻。 – 2013-03-13 21:11:05