2012-06-17 3 views
0

스택 오버플로에 오래 된 스레드는 흔한 이메일을 채우기 위해 자바 스크립트를 사용하는 방법에 대해 설명합니다 :수 없습니다

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 + " 
'"; 

이 유효 자바 스크립트를하지 않습니다. "종결되지 않은 문자열 상수"오류가 발생합니다. 대신이 시도 :

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

원본의 한 줄입니다. Stack Overflow의 편집자가 그것을 munged. 예제를 수정했습니다. – user1461450

+0

글쎄, 생성 된'href'로 실제로 아무 것도하지 않는 것 같습니다. 'submit.href' 대신에'location.href'를 설정해보십시오. –

1

당신은 두 가지 문제가있다 :

  1. button 요소 href의 (HTML4, HTML5)가 없습니다. 하나를 설정해도 아무 것도하지 않습니다. 당신의 제출 핸들러의 끝에서, 당신은 대신 document.location.href을 설정해야합니다 : 당신은 자바 스크립트 문자열에서 문자 줄 바꿈을 가질 수 없습니다

    document.location.href = "mailto:[email protected]?Subject=" + subject + "&Body=" + body; 
    
  2. . 대신 \n 사용

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

알고 ...

  1. 당신은 당신의 이벤트 핸들러에서 이벤트 객체를 받아 들여야하고,에, 당신의 이벤트 핸들러에서 false를 반환하는 대신 event.preventDefault() 전화 양식 제출을 중단하십시오.

  2. 이라는 기능은 없지만 addEventListener도 아니고 attachEvent도없는 경우 사용하고 있습니다.

+0

고마워요! 주된 문제 # 1은이 주제에 대한 원문에서 복사 한 코드가 작동하지 않게 된 이후의 나의 시도였다. 이 코드에서 OP는 window.location.href에 링크를 설정합니다. document.location.href에서 설정하면 작동합니다. 주요 문제 # 2 스택 오버플로 편집기로 인해 발생했습니다.내 원래의 한 줄이야. # 1에 대해 알고 계신 것을 고맙습니다. 저는 그것에 대해 몰랐고 그것을 추가했습니다. 다른 스크립트에서 이벤트 처리 코드를 복사하고 모든 참조를 수정하지 않아서 알아야 할 나머지 작업이있었습니다. 테스트에 걸렸을 것입니다. – user1461450