Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:52:01+00:00 2026-05-26T00:52:01+00:00

So I have an iframe that has some functions wrapped in a $(document).ready(function(){}) like

  • 0

So I have an iframe that has some functions wrapped in a $(document).ready(function(){}) like this.

<script>
    $(document).ready(function(){
        function foo(){
            // do some stuff
        }
    });
</script>

I know that I can access the DOM with something like $(‘iframe’).contents().find(‘something’), but can I access the foo() function from above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:52:02+00:00Added an answer on May 26, 2026 at 12:52 am

    You cannot access the variables in that ready function of the iframe for the same reason that you cannot access local variables of any other function (you can’t “go down” in scope). If you can modify the code in the iframe, however, you can always go up in scope. So in your iframe code:

    $(document).ready(function() {
        function func1() {
            alert("Function in iframe");
        }
    
        parent.iframeFunctions = {
            test: func1
        }
    });
    

    The next problem is that you need to be able to access the function. You cannot use the normal jquery document ready, because the iframe is not finished loading when the parent page’s DOM is ready and therefore your iframeFunctions object is still undefined in the parent page. You need this:

    $(window).load(function() {
        iframeFunctions.test();
    });
    

    You can, however, access the global space of the iframe. Oh yeah, so far this is all assuming that the iframe source falls within the same domain policy. If not, there’s a lot more work involved, and in a lot of cases it’s not even worth it. The jquery object, for example, is in the global space, so here’s how you get at the iframe’s jquery:

    $("iframe")[0].contentWindow.$("#divInIframe")
    

    So, if you modified the content of the iframe source to use function expressions rather than function declartions:

    var func1 = function() {
        alert("Function in iframe");
    }
    
    $(document).ready(function() {
        func1();
    });
    

    You’d be able to access it from the parent as well (after the window load event has fired):

    $(window).load(function() {
        $("iframe")[0].contentWindow.func1();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .jsp that contains an IFrame with a page that has some
I have a page that has an iframe From one of the pages within
I have a page which has an Iframe. In that Iframe, there are six
I have an iframe that returns data to the top.document window just fine by
I have an iframe window that displays a report. To access this iframe, I
I have a function that i need to call on iframe mousemove(). But i
Basically, I have an iframe embedded in a page and the iframe has some
I have an iframe-based online help system that has worked well for years. With
I have an app that has an IFrame with a page flash overly allowing
So I have a page on facebook that has some tabs via apps that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.