2013-10-04 38 views
0

我有這段代碼。用代碼替換url中的空間

我所建立促進我的網站上的頁面。

<script ... > 
document.write('<a href="mailto:?subject=my%20subject&body='+top.location.href+'">mail this link to a friend</a>'); 
</script> 

但是,在我的網址中,我的大部分廣告系列都有空格。這打破了我的促銷電子郵件中的網址,因此到第一個空間的http://超鏈接。

如何更換在上述意義上的代碼空間代碼,整個鏈接將在電子郵件超鏈接在我的網址的空間?

+1

你要替換'top.location.href'的空間?簡單如此:'top.location.href.replace(/ \ s/g,'%20')' –

回答

0

你必須先編碼網址,例如。使用encodeURIComponent函數。

encodeURIComponent(top.location.href); 
0
<script ... > 
var url2 = encodeURIComponent(top.location.href); 

document.write('<a href="mailto:?subject=my%20subject&body='+url2 +'">mail this link to a  friend</a>'); 

+0

有關這種工作方式的一些信息將有所幫助.... – Johan

1
document.write('<a href="mailto:?subject=my%20subject&body=' + encodeURIComponent(top.location.href) + '">mail this link to a friend</a>');