2012-06-29 49 views
1

我發現的代碼一個不錯位添加一些「正常」的文本鏈接(即,而不是一個按鈕),社交媒體網站上共享一個頁面:的Javascript替換所有實例

<a href="https://twitter.com/share?url=[url]"onclick="this.href = this.href.replace('[url]',window.location)" target="_blank" title="Share on Twitter">Twitter</a> 

偉大的工程。但對於Pinterest我需要兩次URL放置,頁面URL和針圖像:

<a href="http://pinterest.com/pin/create/button/?url=[url]&media=[url]pin.jpg" onclick="this.href = this.href.replace('[url]',window.location)" target="_blank">Pinterest</a> 

在這種情況下,只是第一個URL被添加。

不是那麼熱的JavaScript。必須有一種簡單的方法來替換[url]的所有實例,對吧?

回答

3

使用正則表達式與g(全球)修改

this.href.replace(/\[url\]/g, window.location) 
+0

並不十分好看,但就像一種享受。謝謝! –

2

使用全局RegExp匹配他們都:

this.href = this.href.replace(/\[url\]/g, window.location) 
相關問題