4

I have a page which contains another page embedded inside an object tag. Now I want to call a javascript function from the "parent"-page. To be specific: The parent page wants to call a function, which resides inside the embedded code. Former I used iframes but they caused some nasty bugs with Firefox, so I don't want to use them anymore.

Edit: So, my question is: what would be the best way to achieve this, when using an object tag?

Here's some example to illustrate what I want to do:

I have a HTML page "parent.html" inside this page there is some Javascript inside a tag. This parent.html also has an tag and the src of this tag is another HTML page, let's call it child.html. The child.html page has something like:

Here's some pseudo code:

in Child.html:

<script type="text/javascript" src="child.js"></script>

in Child.js:

function getSomething(){
    return something;
}

in Parent.html:

<object id="childObject" data="child.html" width="850" height="510">
</object>

<script>
    // Here I want to access the function from the child.js
    // I tried something like this, but it doesn't work:
    var something = document.getElementById('childObject').contentDocument.getSomething();
</script>

Now: In the Javascript inside the parent.html I need to call a function from the child.js. How can I achieve this?

Thank you :)

4
  • Do you have a question to ask or a problem to show us? As is, this is more of an update on your status. Commented May 4, 2015 at 14:51
  • sorry it is not question Commented May 4, 2015 at 14:52
  • Provide some code to illustrate your problem; besides, even better if you can post what you have done so far in achieving what you expect to have as the solution. Commented May 4, 2015 at 14:57
  • Sorry guys, I was too tired yesterday to realize I forgot to write down the question after briefly explaining the problem :) I hope you can understand, what I want to do and what my question is. Commented May 5, 2015 at 9:23

2 Answers 2

1

Using contentWindow instead of contentDocument works for me:

<object id="childObject" data="child.html" width="850" height="510">
</object>

<script>
    var something = document.getElementById('childObject').contentWindow.getSomething();
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You Can try This

<object id="childObject" data="child.html" width="850" height="510">

<script>
var something = document.getElementById("childObject").parentElement.getSomething();
</script>

1 Comment

This didn't work and I think it's not really what I want to do, because my js calling the .getSomething() is loaded in the parent.html, but thanks for your response :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.