2012-10-16 3 views
1

onclick 함수를 사용하여 제출 버튼에 Google 애널리틱스 트랙 이벤트 코드를 추가하고 싶습니다. 그러나 이미 onclick 함수가 있습니다.하나의 제출 버튼에서 두 개의 onclick 기능

내가

onclick="_gaq.push(['_trackEvent', 'Contact Page Enquiry', 'Enquiry', 'Contact Page Enquiry']);" 

를 추가 할 코드는이 작업을 수행 할 수

<input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','address1','','R','address2','','R');return document.MM_returnValue" value="Submit" /> 

코드 기존?

답변

1

난 그냥 추측하고있어,하지만 ... 당신이 바로 반환하기 전에 코드를 붙여 넣으면?

+1

고마워요.하지만 시도해 봤는데 조지아는 사건을 기록하지 않았습니다. – user1016078

1

jQuery를 사용하는 경우 작동합니다. 단일 요소에 onclick 이벤트를 여러 개 가질 수 있습니다.

확인 this

코드

document.getElementById("a").onclick = function() { 
    alert(2); //this over writes the onclick on the element. This is called 
}; 

$("#a").click(function() { 
    alert(3); //this is called 
});​ 

HTML :

<a id="a" onclick="alert(1)">click</a>​ 

확인 다른 SO 당신이 위의 링크에서 this 또는 this

같은 질문에 이런 식으로 뭔가를 할 수 있습니다 :

<input name="Submit" type="submit" class="button" style="float:left; clear:both;" onclick="MM_validateForm('name','','R','contact_number','','R','email','','RisEmail','address1','','R','address2','','R'); _gaq.push(['_trackEvent', 'Contact Page Enquiry', 'Enquiry', 'Contact Page Enquiry']); return document.MM_returnValue" value="Submit" /> 

이것은 테스트되지 않았습니다. 조심하는

0

두 가지 :

  1. 당신은 아마 단지 양식 유효성 검사를 통과하면 _trackEvent 호출을 트리거합니다.
  2. 이벤트 추적은 웹 로그 분석 서버에서 추적 픽셀을 요청하여 작동합니다. 양식 제출이 추적 요청이 완료되기 전에 동일한 창에 새 페이지를로드하는 경우 누락되거나 일부 데이터 수집으로 끝날 수 있습니다.

두 번째 문제는 양식 제출을 지연시킴으로써 해결할 수 있습니다.

<input id="Submit" name="Submit" type="submit" ... /> 
... 
$('#Submit').click(function(e) { 
    // Prevent the default handling of the form submit button 
    e.preventDefault(); 
    MM_validateForm('name', ... 
    // If the form didn't validate, don't do anything else 
    if (!document.MM_returnValue) return; 
    _gaq.push(['_trackEvent', ... 
    // Get the form & submit it after 150ms delay 
    var form = $(this).parents('form:first'); 
    setTimeout(function() {form.submit()},150); 
}); 
관련 문제