views:

44

answers:

1

So I'm trying to use javascript to pop up a video, and need to know when I'm in an iframe so I can resize the video accordingly.

When outside the iframe, I see three alerts ('this is seen by both', 'not in iframe', and 'this is after the fact')

When in the iframe, I just see the first alert ('this is seen by both').

var handleViewVideoClick = function (dataAction, eventType, targetElement, dataActionElement) {
alert('this is seen by both');
if ($('iframe', window.parent.document).length == 0) {
            alert('not in iframe');
} else {
            alert('in iframe');
}
alert('this is after the fact...');
};

What's going on here?

A: 

My guess would be you're not loading jQuery in that window, resulting in a $ is undefined error. But no worries, you don't need jQuery anyway, just check if the current frame is also the top frame, like this:

if (self != top) {
  alert("I'm in an iframe, :(");
} else {
  alert("I'm freeeeeeeeeeeeeeeeeeee!");
}
Nick Craver
Thanks, that works great!
gr33nh4t
@gr33nh4t - Welcome:) and welcome to SO!
Nick Craver