2012-07-13 53 views
0

我想使用AJAX和jQuery将我的网页加载到我的网站上。它工作正常,但该URL的长相扣除哈希后的完整网址

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

我想减去URL,这样它看起来像

HTTP ://mywebsite.com/#thepage

我希望有人知道如何做到这一点。这是代码:使用正则表达式Java中的一个简单的方法

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'); 

回答

0

这里:

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"); 
+0

但是它的外观在我的代码? de url是可变的。 – Giel 2012-07-13 11:24:50

+0

只需将上面的'url.replace..'语句中的url变量替换为您的变量。 – Keppil 2012-07-13 11:27:57