0

I was wondering if it's possible to load a bit of JavaScript from within an iframe. It seems that this questions has been answered here.

However, the reason I ask is that the iframe is loaded from an external source (i.e. an adserver) and then get's the time from the users machine. What I am trying to do is calculate how long is spent on the page when this iframe is loaded (and then potentially pass this value back to a server).

I know there aren't any code samples in this questions but if someone has come across something like this before please let me know. I always thought there would be some sort of security issue with this i.e. calling an iframe on a page and then that iframe running a script which effectively "gets" something (even if it's just the time) from your machine.

Thanks

Community
  • 1
  • 1
zik
  • 3,035
  • 10
  • 41
  • 62

2 Answers2

1

You cannot communicate with the contents of an Iframe that is served by a different domain due to the Same Origin Policy. This is a security feature that prevents framed content from taking over the host page and other types of scripting attacks.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Awesome, thanks for this; I thought as much. Just had a quick read of these. So if I was to say count how long a page had been loaded for, I would need to have that as a script on that particular page? – zik Jan 31 '12 at 15:26
0

The iframe has an onload event which fires when the contents have finished loading. You could grab the time that your page loaded at and then subtract it from the time that the iframe's onload event finishes.

Dan Iveson
  • 926
  • 6
  • 10
  • Cool thanks, this is the kind of thing I was thinking of but what I am actually trying to do is determine how long the parent page (which contains the iframe) was loaded for, from within the iframe. But thanks all the same – zik Jan 31 '12 at 15:28