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

  • SEARCH
  • Home
  • 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 8308701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:53:09+00:00 2026-06-08T18:53:09+00:00

Edit: Just found out this is a chrome problem, the code works fine in

  • 0

Edit: Just found out this is a chrome problem, the code works fine in firefox

I have an iframe on a webpage that shows a book formatted as html. I would like to insert some javascript within this iframe to make the book more dynamic (e.g. click on sentences, show animations etc). The iframe content is in the same domain as the parent page.

I can insert the javascript into the iframe but get an error calling a function in the inserted javascript. I’ve described the different bits of code below:

My parent page javascript is:

function iframeLoaded()
{
    var iFrameID = document.getElementById('preview-iframe');

    var jsLink = iFrameID.contentDocument.createElement("script");
    jsLink.src="/tests/iframeAPI.js";
    jsLink.type = 'text/javascript';
    iFrameID.contentDocument.head.appendChild(jsLink); 
    iFrameID.contentWindow.initialiseApi()
}

and the html containing the iframe is:

<iframe id="preview-iframe" width="640" height="240" frameborder="0" src="./testpage.htm" onload="iframeLoaded()" scrolling="no"></iframe>

The contents of iframeAPI.js is:

window.initialiseApi = function() { alert("Hello world") }

Looking at the iFrame’s html in the browser shows that the iFrameAPI.js tag is inserted ok into the iframe head, but I don’t get the alert popup when the page is loaded. The error appears on the following line:

    iFrameID.contentWindow.initialiseApi()
    Uncaught TypeError: Object [object Window] has no method 'initialiseApi' 

However I can run this line in the browser’s javascript console and the alert popup works fine.

Any help would be much appreciated.

Thanks,

Brian


Edit: I’ve just tried with an onload event to make sure the page is loaded and I still have the problem:

My parent page javascript is now :

function iframeLoaded()
{
    var iFrameID = document.getElementById('preview-iframe');

    var jsLink = iFrameID.contentDocument.createElement("script");
    jsLink.src="/tests/iframeAPI.js";
    jsLink.type = 'text/javascript';
    iFrameID.contentDocument.head.appendChild(jsLink); 
    jsLink.onLoad= iFrameLoaded();
}

function iFrameLoaded()
{
    alert("Iframe loaded"); // Alert works ok 
    var iFrameID = document.getElementById('preview-iframe');
    iFrameID.contentWindow.initialiseApi(); // Same error message on this line
}
  • 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-06-08T18:53:12+00:00Added an answer on June 8, 2026 at 6:53 pm

    It sounds like you are trying to use the function before the content has loaded.

    try this instead:

    var t = setTimeout(iFrameID.contentWindow.initialiseApi(),500);
    

    This will wait half a second before trying the function which should give the page tiem to load. Delay times are given in milliseconds.

    An even better approach is to try using Jquery and its ready() method but this requires the jquery library to be loaded as well. Its well worth it though in my opinion, see http://api.jquery.com/ready/.

    You would try something like:

    $("body",iFrameID.contentWindow.document).ready(iFrameID.contentWindow.initialiseApi())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done some research on this subject, and found out, that Gzipping and
Edit: to clarify - i just found out that it is really easy to
EDIT: Just to make things clear, this problem was caused by a typo in
EDIT 2: I have just realised that my HTML is only displaying the artist
** EDIT ** Nevermind, just needed to take out the parens... I get this
I just found out that the Screen Capture by Google extension makes my website's
I have a webpage that on a button click grays out the main form
Edit: On further examination Firefox does not seem to be doing this, but Chrome
Sorry If this is trivial, Edit: in short I found out this really isn't
Edit: just to be clearer on what I am looking to do. I have

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.