Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Getting Javascript variable from a source script on an html page loaded in an iframe

Following up from this question here: Accessing JavaScript variable in an iframe, from the parent window on same domain

For purposes of posting something testable here, I have a src.js file set up like:

var foo = {"foo":"bar"};

then I can an HTML page test1.html with this line in the head:

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

Then I have another HTML page test2.html, with

<iframe src="test1.html"></iframe>

How can I access foo from test2.html? I tried the following but could not find foo (running this on button click from a div in test2.html)

  console.log("foo: " + typeof foo);
  console.log("foo: " + window.foo);
  console.log("Frame window: " + window.frames[0].window); //this works
  console.log("Sections: " + window.frames[0].window.foo);

Note: In the iframe, foo is definitely there

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is what eventually worked for me. In JS library:

const foo = {"foo":"bar"};

window.getFoo = function() {
  return foo;
}

On page with iFrame (test2.html):

  var iframe = document.getElementById("mainPanel"); //mainPanel is the id of the iframe
  var iWindow = iframe.contentWindow;
  var foo = iWindow.getFoo();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!