2012-06-17 32 views
0

Stack Overflow上一個古老的線程討論如何使用JavaScript來填補一個mailto電子郵件:無法發送電子郵件,其中包含的JavaScript

Sending emails with Javascript

我感興趣的應用技術,但不能把它上班。

在下面的代碼中,當我在makecontact()方法中設置一個返回false的斷點並查看它記錄的URL時,它看起來很好。

但瀏覽器不打開電子郵件客戶端。

如果我在提交按鈕中的href中硬編碼相同的URL,那麼它將啓動電子郵件客戶端。

爲什麼不設置href的工作?

答案:這是錯誤的href。

修正版本:

<!-- TODO: Validate name and text fields and don't allow submit until they are valid. Limit total mailto URL length to 2000. --> 
<form name="contact"> 
<br/>Reason for contact: 
<br/> 
<input type="radio" name="reason" value="Product Inquiry/Presales Questions" checked="checked"/>Product Inquiry/Presales Question<br/> 
<input type="radio" name="reason" value="Support/Warranty"/>Support/Warranty<br/> 
<input type="radio" name="reason" value="Feedback"/>Feedback<br/> 
<input type="radio" name="reason" value="Other"/>Other<br/> 
<input type="text" name="name" id="name"/>Your name:</div> 
<textarea name="contacttext" rows="20" cols="60" id="contacttext"></textarea> 
<button id="submit">Submit</button> 
</form> 
<script type="text/javascript" id="contactjs"> 

<!-- 
var submit = document.getElementById("submit"); 

function getreason() { 
    var radios, i, radio; 

    radios = document.getElementsByName("reason"); 

    for (i = 0; i < radios.length; i += 1) { 
     radio = radios[i]; 
     if (radio.checked) { 
      break; 
     } 
    } 

    return encodeURIComponent(radio.value); 
} 

function makecontact(e) { 
    var subject, name, text; 

    subject = getreason(); 
    name = document.getElementById("name").value; 
    text = document.getElementById("contacttext").value; 
    body = "From: '" + name + "', Content: '" + text + "'"; 
    body = encodeURIComponent(body); 

    document.location.href = "mailto:[email protected]?Subject=" + subject + "&Body=" + body; 
    console.log(document.location.href); 

    e.preventDefault(); 

    return false; 
} 

if (submit.addEventListener) { 
    submit.addEventListener("click", makecontact, true); 
} else if (form.attachEvent) { 
    submit.attachEvent("onclick", makecontact); 
} else { 
    submit.click = makecontact; 
} 
//--> 
</script> 
</div> 
+0

您應該添加正確的解決方案作爲答案,而不是編輯它到您的文章。 –

回答

3
body = "From: ' 
" + name + " 
', Content: ' 
" + text + " 
'"; 

這不是有效的JavaScript。它會導致「未終止的字符串常量」錯誤。試試這個:

body = "From: '\n" + name + "\n', Content: '\n" + text + "\n'"; 
+0

這是原始單行。 Stack Overflow的編輯使用它。修正了這個例子。 – user1461450

+0

那麼,你似乎並沒有對生成的'href'做任何事情。嘗試設置'location.href'而不是'submit.href'。 –

1

你有兩個主要問題:

  1. button元素沒有href S(HTML4HTML5)。設置一個不會做任何事情。在您提交處理結束,則應該設置document.location.href

    document.location.href = "mailto:[email protected]?Subject=" + subject + "&Body=" + body; 
    
  2. 你不能在JavaScript中的字符串字面換行符。使用\n代替:

    body = "From: ' \n" + name + " \n', Content: ' \n" + text + " \n'"; 
    

也知道......

  1. 你應該接受你的事件處理程序的事件對象,並調用event.preventDefault()而不是僅僅返回false從事件處理程序,以停止提交表單。

  2. 不存在稱爲resume的功能,但是如果addEventListenerattachEvent都不存在,則使用它。

+0

謝謝!主要問題#1是我在嘗試從關於此主題的原始文章複製的代碼無法工作之後使其工作的嘗試。在該代碼中,OP在window.location.href中設置鏈接。在document.location.href中設置它。主要問題#2是由Stack Overflow編輯器引起的。這是我原來的一行。謝謝你要注意的#1,我不知道這件事,並加入了它。剩下要注意的事情是由於從另一個腳本中複製事件處理代碼而沒有修復所有的引用。在測試中會發現這一點。 – user1461450