English 中文(简体)
How to track the user in an iframe
原标题:

I want to track what happens inside an iframe when a user clicks on links in the IFrame. The page that contains the iframe (the parent) is to track the user´s navigation through the page in the iframe. Both pages will be hosted on the same toplevel domain, although the subdomains will differ.

I need the parent page to be notified of every click, but I do not have direct control over the pages I load into the iframe.

Is adding an onclick to all the links whenever the page in the iframe is loaded possible? How would I go about doing this?

This would be the "template" on which to build:

<html>
 <script language="javascript">
     var currentURL;
 </script>
 <body>
     <iframe id="container" width="500" height="500" src="http://subdomain.parentdomain.com"/>
 </body>

最佳回答

I am going to use an AJAX-Proxy to have the content (as far as the browser is concerned) come from my domain. This will solve all the cross domain scripting issues that CodeJoust mentioned. Speed of delivery might be a problem due to the overhead I will be generating, but that will have to be seen.

I will probably move along the lines of this Stackoverflow Question:

"Apply “onclick” to all elements in an iFrame"

Regarding legal issues pertaining to proxying and changing the content of pages dynamically, it will have to be checked. I believe that tracking users that give their express consent is, from an ethical standpoint, unproblematic.

问题回答

Iframe CrossDomain access needs to be the same domain, subdomain, and port.

If you had them on the same domain, you could bind click event handlers on all the links, then when they are clicked log a click to something like google analytics, your database, etc.

I do not have direct control over the pages I load into the iframe

That s your blocker. If you can augment the code within the remote pages, you can use postMessage and the iframe fragment identifier hack to get browser coverage. Fortunately, someone already did the dirty work for you:

With jQuery it would be easy.

$(document).ready(function(){
   var iframeWindow = $( #container )[0].contentWindow;
   $(iframeWindow).load(function(){
       $(this).find( a ).click(top.myClickHandler);
   });
}

function myClickHandler(){
    /* Do something */
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签