在哈希之后减去完整的url

时间:2012-07-13 10:44:13

标签: javascript jquery ajax hash location

我想用AJAX和jQuery在我的网站上加载我的页面。它工作正常,但网址看起来

http://mywebsite.com/#mywebsite.com/thepage

我想减去网址,使其看起来像

http://mywebsite.com/#thepage

我希望有人知道该怎么做。这是代码:

if(window.location.hash) {

} else {
  window.location="#THE_FULL_URL";
}

var newHash      = "",
    $mainContent = $("#wrapper"),
    $pageWrap    = $("#wrapper"),
    $el;

$("a").live("click", function(event){
    window.location.hash = $(this).attr('href');
    return false;
});

$(window).bind('hashchange', function(){

    var hash = location.hash;
    document.title = ( hash.replace(/^.*#/, '') || 'blank' ) + '.';

    newHash = window.location.hash.substring(1);

    if (newHash) {
        $mainContent
            .find(".content")
            .fadeOut(200, function() {
                $mainContent.hide().load(newHash + " .content", function() {
                    $mainContent.fadeIn();
                    fix();
                });
            });
    };

});

$(window).trigger('hashchange');

1 个答案:

答案 0 :(得分:0)

这是Java中使用正则表达式的一种简单方法:

String url = "http://mywebsite.com/#mywebsite.com/thepage";
String newUrl = url.replaceAll("^(http://)([^#]*)#\\2(.*)$", "$1$2#$3"));

这里是Javascript:

var url = "http://mywebsite.com/#mywebsite.com/thepage";
var newUrl = url.replace(/^(http:\/\/)([^#]*)#\2(.*)$/g, "$1$2#$3");