2011-11-02 99 views
9
當前頁面

寫我在網站的所有網頁mailto按鈕,我想要寫在電子郵件正文中引用該頁面。如何在「郵寄地址」體鏈接

在一個頁面上,我可以有

<a class="email" title="Email a friend" href="mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0ahttp://www.a.com/Home/SiteMap/tabid/589/Default.aspx">Email</a> 

但我怎樣才能使這個通用的所有網頁?

+0

Javascript可以爲你做這個。通過使用「document.location.href」。或者,如果您使用.php,則可以更輕鬆地完成。你可以使用PHP?讓我知道,我可以爲你做一個例子! –

+0

不,我使用Asp;) 但是我怎麼能在那部分寫javascript?(body =「...「) –

回答

31

這是純JavaScript解決方案:

<a class="email" title="Email a friend" href="#" onclick="javascript:window.location='mailto:?subject=Interesting information&body=I thought you might find this information interesting: ' + window.location;">Email</a> 
+0

這可能會導致頁面改變。 ... + window.location; return false;「> ... –

+1

@TravisSwientek你能解釋爲什麼會發生這種情況嗎? –

+0

這會在當前窗口中打開電子郵件客戶端。任何方式,我可以使它在新標籤中打開?謝謝。 – NJT

3

您可以在每個頁面上放置一個JS,以便在文檔加載時修改所有電子郵件鏈接的所有hrefs。

我會使用jQuery的示例代碼,因爲它更緊湊,但也可以用純JS來實現:

$(document).ready(function(){ 
    $(".email").attr("href", "mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0a"+window.location); 
}); 
+1

當然,它可以實現純粹的JavaScript :),什麼是jQuery? – david

+2

@david:」jQuery是一個快速簡潔的JavaScript庫,簡化了HTML文件遍歷,事件處理,動畫和Ajax交互以實現快速Web開發。 jQuery的目的是改變你寫JavaScript的方式。「 –

+0

感謝尼克,我只是在挖你:}什麼是jquery' javascript – david

1

你需要使用&body=在郵件正文中包含一個字符串

這裏是一個通用的javascript函數

<script> 
    function emailFriend(){ 

    var strrep ,ptitle = document.title; 

strrep= ptitle.replace(/"/g,'%22'); 
strrep= ptitle.replace(/&/g,'%26'); 

var mailtourl = "mailto:?subject=Interesting%20information&body=I thought you might find this information interesting: "+encodeURIComponent(location.href); 
location.href = mailtourl; 
return false 
} 

</script> 


<a href="javascript:;" onclick="emailFriend();return false">Email</a> 
8

使用Javascript,您可以在UTF-8頁面上使用encodeURIComponent()對utf-8-percent-encode編碼主體和主體hfvalue。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title></title> 
     <script> 
      function SendLinkByMail(href) { 
       var subject= "Interesting Information"; 
       var body = "I thought you might find this information interesting:\r\n\r\n<"; 
       body += window.location.href; 
       body += ">"; 
       var uri = "mailto:?subject="; 
       uri += encodeURIComponent(subject); 
       uri += "&body="; 
       uri += encodeURIComponent(body); 
       window.open(uri); 
      } 
     </script> 
    </head> 
    <body> 
     <p><a href="javascript:(function()%7BSendLinkByMail()%3B%7D)()%3B">Email link to this page</a></p> 
    </body> 
</html> 

如果你在做這個服務器端,你可以建立mailto鏈接併發送它作爲href屬性值。然後,你根本不需要JS。

我假設ASP有一些像encodeURIComponent()一樣工作的URI編碼函數。

您還可以查看我的mailto URI composer頁面的源代碼作爲另一個例子。

你的也看看http://shadow2531.com/opera/testcases/mailto/mailto_uri_scheme_idea.html#send_link_by_mail和我mailto URI syntax validator

對於我在上面的JS代碼中包含URI的<和>,請參閱RFC3986中的「附錄C.在上下文中定界URI」。

此外,而不是window.location.href,可以使用window.location的或document.location.href或document.location。我通常使用「document.location」。

爲什麼你使用JavaScript URI而不是onclick屬性,請參閱this answer

另請注意,在上述代碼中的JS URI中,我將代碼封裝在一個匿名函數中。在這種情況下這不是必需的,因爲該函數不會返回任何會在單擊時更改文檔的內容。但是,這只是爲了好的措施而始終做到這一點。

見我Javascript URI compose,以幫助創建的JavaScript的URI。

+1

不錯的mailto驗證Shadow2531 – david

+0

有沒有辦法阻止這打開一個新的空白標籤/窗口? – Jason

+1

@Jason您可以將window.open(uri)更改爲window.location.href = uri – Shadow2531

相關問題