jQuery Communicating Between Page and iFrame

I have an iframe on a page that are both loaded from same domain, though iframe on a subdomain. I want to try one of two things:

  1. Have the parent run a function in the iframe once a user scrolls down.
  2. Have the iframe listen for parent scroll and run the function itself.

Which is the better of the two?

Cheers!
Ryan

I don’t know if there’s much difference, although it seems natural to have the logic in the parent where it’s possible.

Are you asking how to do this? ^^

I’m just looking for best approach, but thinking something like:

$(window).on('scroll', function() {
$('#iframeid')[0].contentWindow.insidefunction();
}

That work?

Cheers!
Ryan

Actually, I’m struggling.

I have two things:

if ( $(".embed-ta").length ) {
 
            alert("element exists");
}

function playta() {
                $('iframe .embed-ta')[0].contentWindow.playerInstance.play(true);
            }

The top one is on document.ready, and it does the alert, the second is just a function. I try to call the function from the console – playta() – and it says ReferenceError: playta is not defined. How is this possible?

Cheers
Ryan

Is playta() defined inside of another function?

Shit, you are right. it was.

I’m trying to run the…

//inside the iframe I have

function playinner() {
console.log(‘outer play’);
playerInstance.play();
}

//in the parent I have

function playta() {
$(‘.embed-ta’)[0].contentWindow.playinner();
console.log(‘this ran outside’);
}

I get a javascript error saying permission denied to playinner(). Same domain, different subdomain.

You should be able to set document.domain on each page to the same domain.
For example, with the subdomain page, set its domain to be the same as the other page that you’re wanting to access.

1 Like

Yep, that did it! thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.