Search code examples
javascriptpdfonclickspotlightdocument.write

Javascript floodlight tracking iframe leaves browser window blank, please help!


I have been asked to implement a javascript floodlight tag onto my site which is to be called everytime a customer downloads a pdf file. I have tried to implement this as follows:

<script type="text/javascript">
function appForm() {
 var axel = Math.random() + "";
 var a = axel * 10000000000000;
 document.write('<IFRAME SRC="http://fls.doubleclick.net/activityi;src=1234567;type=count123;cat=123do456;ord=1;num='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
 return false;
}
</script>
<a href="appForm.pdf" target="_blank" onClick="appForm();">Download PDF</a>

This seems to almost work. The pdf file opens in a new window. But the window where you clicked the download link turns blank. Is there a way of opening this tracking iframe and then having the pdf opening without the parent window going blank? Your help will be very much appreciated on this.


Solution

  • document.write() can't be reliably used after the page has loaded. Use the more modern document.createElement function and its friends:

    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", "http://...");
    iframe.setAttribute("width", "1");
    iframe.setAttribute("height", "1");
    iframe.setAttribute("frameborder", "0");
    document.body.appendChild(iframe);
    

    (untested, but seems like it should work...)