2012-03-02 27 views
0

我有一個這樣的URL:的JavaScript:查找和替換,而不刷新URL的一部分,HTML5 history.replacestate

http://site.com/images/961b7d0e50f0462a8828d31bf0874a71/lightbox 

我需要string.format替換GUID從陣列與另一GUID的URL(下一個或上一個),不點擊事件刷新。

HTML:

<a id="replaceGUIDnext">Replace GUID to previous in Array</a> 
<a id="replaceGUIDprevious">Replace GUID to previous in Array</a> 

SCRIPT:

var guids = ['3ffffb5fb7424c3d96f361fc18a753e0', '961b7d0e50f0462c8828c31bf0874a71', '62a96a75f4764c9ea3d9b65a47dce53d']; 
var baseURL = http://site.com/images/{1}/lightbox 

$('#replaceGUIDnext').click(function(){ 
    ...something goes here 
}); 

$('#replaceGUIDprevious').click(function(){ 
    ...something goes here 
}); 

聲明{1},它應與的String.Format代替。

我想使用HTML5 history.replacestate。如果瀏覽器不支持它,它應該忽略替換(或故障恢復到正在運行的解決方案)。

+0

好的,你在MDN上查了'replaceState'嗎? - https://developer.mozilla.org/zh/DOM/Manipulating_the_browser_history – 2012-03-02 13:17:16

回答

0

JavaScript沒有string.format,所以只需使用replace。

function changeIt(guid) { 
    if(history.replaceState) { 
     var newUrl = baseURL.replace("{1}", guid); 
     history.replaceState({ "guid" : guid}, guid, newUrl); 
    } 
}