2013-09-16 52 views
1

我有一個包含參數的URL。我試圖創建一個mailto命令來打開當前窗口URL的鏈接。我目前使用下面的JS來調用該函數,但它會在「&」符號之後截斷URL。從URL傳遞參數到mailto命令

<a href="mailto:?subject=Report&body=[sub]" onclick="this.href = this.href.replace(''[sub]'',window.location)">email</a> 

這將返回「http://www.mydomain.com/reportname?SiteCode=0027」在電子郵件的正文中,但URL實際上是下面列出,因爲它是傳遞變量: http://www.mydomain.com/reportname?SiteCode=0027&CustomerCode=ALLCUSTOMERS&JobCode=ALLJOBS

我怎麼能輸出完整的URL包括參數的電子郵件?

回答

1

我做了一個小小的jsFiddle。我做了兩個小改動。

  1. 首先我改變href屬性的onClick,並沒有把它放在DOMContentLoaded
  2. 其次,我不得不使用encodeURIComponent方法的任何特殊字符會打破你的腳本

現金去this questiondr. google


<html> 
<head> 
    <script type="text/javascript"> 
     document.addEventListener("DOMContentLoaded", function() { 
      var simpleHref = document.getElementById("simpleHref"); 
      simpleHref.href = "mailto:[email protected]?subject=Report&body=" + encodeURIComponent(window.location); 
     }); 
    </script> 
</head> 
<body> 
    <a href="" id="simpleHref">Test</a> 
</body> 
</html> 
+0

謝謝!這工作像一個魅力。 – user2470781