나는 추적 픽셀 JS 기능을 204 "no_content"응답을 사용하도록 변환하기 위해 며칠 동안 노력해 왔습니다.
이 작업을 쉽게 수행 할 수 있지만 나중에 콜백 함수를 실행할 수 있어야합니다.
다음은 204가 반환되면 해고되지 않는 것 같습니다.
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
error: function () {
console.log('error');
},
success: function () {
console.log('success');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
if (beacon.onerror) {
beacon.onerror = opts.error;
}
if (beacon.onload) {
beacon.onload = opts.success;
}
$(beacon).on('load', function( response, status, xhr ){
alert(status);
});
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
추적은 제대로 기록되지만 경고 또는 콘솔 로그 메시지는 없습니다. 이것이 가능합니까 아니면 단지 시간을 낭비하고 있습니까?
편집하다 ------
아래의 솔루션을 기반으로 최종 버전이 있습니다 (이는 오류와 성공 모두 동일한 콜백을 사용한다고 가정합니다.
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
callback: function () {
console.log('tracked');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
$(beacon).on('error success done', opts.callback);
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
이미지에 콜백을 첨부하지 않습니다. if (beacon.onerror)
이므로 테스트 결과가 거짓 beacon.onerror
입니다 null
.
당신은 사용해야 if( "onerror" in beacon )
여부를 테스트에 beacon
이 onerror
속성을.
하지만 왜 jquery의 방법을 사용하지 on
않습니까?
$(beacon).on("error", function() {
alert("Jquery error");
});
$(beacon).on("done", function() {
alert("Jquery done");
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다