2016-11-14 21 views
1

我的公司希望從廣告提供機構獲得一些流量,他們給了我們一個像素跟蹤代碼並強制我們在10秒後將其加載到網站上。 我試圖通過JS來做到這一點,但仍然失敗。如何在10秒後加載跟蹤像素?

這裏是像素的外觀:

`<img src="https://ext.example.com/conversion/?c=122959&a=30225" width="1" height="1" onerror="this.onerror=null;this.src='https://a248.e.example.net/9e0d7c940412d5badab847b855f8ac46.com/conv/'+this.src.substring(this.src.indexOf('?'));" />` 

這裏是我試圖做的:

var lines = "<img src="; 
var lines2="https://ext.example.com/conversion/?c=122959&a=30225"; 
var lines3=";this.src='https://a248.e.example.net/9e0d7c940412d5badab847b855f8ac46.com/conv/'+this.src.substring(this.src.indexOf('?'));";     
     setTimeout(function(){ 
      document.write(lines+'"'+lines2+'" width="1" height="1" onerror="this.onerror=null'+lines3+'" />'); 
     }, 10000); 

我使用的,因爲變量 - >'< - 符號

10秒後我的所有內容都消失了,源代碼只顯示了Pixel代碼。

您是否有更少的資源消耗解決方案?我如何在10秒後點擊網頁上的像素

+0

可能是格式錯誤的HTML。例如,你缺少'src'值的引號。 Dan的下面的例子比較乾淨,但我很喜歡那個 –

回答

2

純粹的JavaScript實現將在10秒後將跟蹤像素附加到<body>

window.onload = function() { 
    function addTrackingPixel() { 
    var pixel = document.createElement("IMG"); 
    pixel.setAttribute("src", "https://ext.example.com/conversion/?c=122959&a=30225"); 
    pixel.setAttribute("height", "1"); 
    pixel.setAttribute("width", "1"); 
    pixel.setAttribute("onerror", "this.onerror=null;this.src='https://a248.e.example.net/9e0d7c940412d5badab847b855f8ac46.com/conv/'+this.src.substring(this.src.indexOf('?'));"); 
    document.body.appendChild(pixel); 
    } 
    var timeout = setTimeout(addTrackingPixel, 10000); 
}; 
+0

非常感謝! –