2011-02-22 2 views
0

사이트에서 구현할이 JavaScript 코드 스 니펫을 받았습니다. 그냥 붙이면 작동하지 않습니다. 이 코드가 무엇을하는지 이해하면 더 잘 이해할 수 있다고 생각합니다. 누군가이 코드가 대략 무엇을 설명 할 수 있습니까? 감사!JS 코드가 무엇을하고 있는지 잘 모름

<script type="text/javascript"> 
var thisref = document.referrer.replace(/&/g, "zzzzz"); 
var ciJsHost = (("https:" == document.location.protocol) ? "https://" : "http://"); 
document.write(unescape("%3Cscript src='" + ciJsHost + "tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" + new Date().getTime() + "&prev=" + thisref + "' type='text/javascript'%3E%3C/script%3E")); 
</script> 
+0

_ 그걸 붙이면 작동하지 않습니다 ._ 정확히 작동하지 않는 것은 무엇입니까? "그녀가 말한 것은 그게 내 마음 속에 떠오르는 이유"입니다. – epascarello

+0

하하, 고마워. 방금 T.J.의 게시물에 아래에 설명을 올렸습니다. 창문 안에 넣는 데 문제가있어.로드. – RyanPitts

답변

8

을로드하기 위해 두 번째 줄과 세 번째 줄은 script 태그를 문서에 삽입하고 있습니다. 페이지의 프로토콜이 http 인 경우 , https 인 경우 https 인 경우 http을 사용합니다. (more here). 페이지의 리퍼러를 스크립트로로드하는 GET에 전달합니다 (&zzzzz으로 바뀜). munique 매개 변수는 현재 날짜의 숫자 버전을 가져옵니다. 캐싱을 무효화하려는 시도로

unescape 호출과 인코딩 된 문자 이유는 당신이 script 태그 내부의 자바 스크립트 코드의 문자열 내부 </script>이있는 경우는 조기에 스크립트 태그를 오게됩니다. (JavaScript 코드가 자체 파일에 있어야하는 몇 가지 이유 중 하나입니다.) 따라서 대괄호를 인코딩하고 unescape을 사용하여 HTML 파서가 </script>으로 표시되는 것을 피합니다.


업데이트 : 당신은 당신이 window.load에서 그 코드를 사용하려는 말했다 아래. 페이지의 초기 구문 분석 중에 만 document.write을 사용할 수 있기 때문에 그렇게 할 수 없습니다. (나중에 그것을 사용하는 경우, 그것은   — 그것을 지우고 다음 새, 빈 문서에 기록 문서   — 다시 열립니다.)

당신은,하지만, 나중에 다른 메커니즘 (갱신를 통해 script 태그를 추가 할 수 있습니다 : 죄송합니다, 당신이 jQuery를 사용하고 놓쳤다; 맨 아래에 jQuery를 버전)입니다 :

<script type="text/javascript"> 
window.onload = function() { 
    // Get the referrer, replacing `&` with `zzzzz` 
    var thisref = document.referrer.replace(/&/g, "zzzzz"); 

    // Get the protocol to use based on the current document's protocol 
    var ciJsHost = (("https:" == document.location.protocol) ? "https://" : "http://"); 

    // Create a script element, set its type and src 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = ciJsHost 
       + "tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" 
       + new Date().getTime() 
       + "&prev=" + thisref; 

    // Add it just about anywhere; `document.body` works fine and is 
    // easy. The script is downloaded and executed when you do the append. 
    document.body.appendChild(script); 
}; 
</script> 

을 또는 프로토콜에 대한 트릭 I linked to above을 사용하려면 :

<script type="text/javascript"> 
window.onload = function() { 
    // Get the referrer, replacing `&` with `zzzzz` 
    var thisref = document.referrer.replace(/&/g, "zzzzz"); 

    // Create a script element, set its type and src 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = "//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" 
       + new Date().getTime() 
       + "&prev=" + thisref; 

    // Add it just about anywhere; `document.body` works fine and is 
    // easy. The script is downloaded and executed when you do the append. 
    document.body.appendChild(script); 
}; 
</script> 

thisref 부분에는 별도의 변수가 필요하지 않습니다. 그 변화하고 위의 설명 밖으로 제거 : 마지막으로

<script type="text/javascript"> 
window.onload = function() { 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = "//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" 
       + new Date().getTime() 
       + "&prev=" + document.referrer.replace(/&/g, "zzzzz"); 
    document.body.appendChild(script); 
}; 
</script> 

그리고을, 나는 당신이 한판 승부 'jQuery를, 미안 사용하고 있음을 놓쳤다 :

<script type="text/javascript"> 
$(window).load(function() { 
    $("<scr" + "ipt type='text/javascript' src='//tracking.callmeasurement.com/clickx/click_matrix.cfm?munique=" 
     + new Date().getTime() 
     + "&prev=" + document.referrer.replace(/&/g, "zzzzz") + "'></src" + "ipt>") 
     .appendTo(document.body); 
}); 
</script> 

을 ... 오히려 jQuery(...을 고려할 수 있지만, $(window).load(...보다 큰 이미지가 필요합니다. 모든 이미지와로드가로드 될 때까지 기다리는 것이 좋습니다. 그것은 'ZZZZZ'로 앰퍼샌드를 대체하기 위해 참조하는 페이지의 URL을 munges,

+0

설명 답을 보내 주셔서 감사합니다. 정말로 나를 이해하도록 도왔다. 이 스크립트는'window.load'에서 랩핑 (wrapping) 할 때 적합합니다. 왜 그런가? jQuery'$ (window) .load() {'로 감싸는다면 스크립트 태그를 제외한 모든 코드가 제거된다. 그것은'window.load' 내부에 있기 때문입니까? – RyanPitts

+0

음 ... 나는 쓰여지고있는 스크립트 태그가 문제라는 것을 발견했습니다. 위의 코드는 자바 스크립트 태그를 만들고 그 새 탭 안의 코드는'$ (document) .ready (function() {')을 가지고 있습니다. 나는이 문서가 document.ready와 관련이 있다고 생각합니다. document.load? – RyanPitts

+0

@ 라이언 : 페이지의 초기 구문 분석 동안을 제외하고는'document.write'를 호출 할 수 없으므로 대신'window.load'에서 어떻게 수행하는지 보여주기 위해 답변을 업데이트 할 것입니다. –

2

그냥 서버에서 스크립트를로드하려고합니다. 더미 리퍼러 정보도 보내고 있습니다.

1

먼저 ... jQuery를 사용하는 경우에도

는 사실은, 비의 jQuery 버전을 선호하지만, 사람들의 취향이 다릅니다.

다음으로 현재 페이지에 액세스하는 데 사용 된 프로토콜이 HTTP인지 HTTPS인지를 확인합니다.

마지막으로 src 요소를 추적 사이트로 설정하여 스크립트 태그를 생성하고 수정 된 referer URL에 현재 페이지에 액세스하는 데 사용 된 프로토콜과 동일한 프로토콜을 사용하여 시간을 기반으로 고유 한 ID를 제공합니다 .

간단히 말해서,이 페이지의 도착자가 어디에서 왔는지를 결정하는 추적 코드입니다.

관련 문제