[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get variable from iframe

Posted on 2007-08-05
6
Medium Priority
?
6,908 Views
Last Modified: 2013-11-05
I have the structure.

<script type="text/javascript">
function GetValue() {
iF=document.getElementById("main")
alert (iF.m)
}

</script>
</head>
<input type="button" onclick="GetValue()" value="ClickHere">
<iframe id="main" width="100%" height=100% src="somesource.htm" name=mainwindow frameborder=no framespacing=0 marginheight=0 marginwidth=0></iframe>

inside of iFrame is javascript variable defined
var m=25
I wish to get its value in parent document. But the code above doesn't work to me :(  
0
Comment
Question by:APRESTOUS
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Expert Comment

by:TName
ID: 19633710
Hi, you could try something like this:

Main html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script type="text/javascript">
      function getIFVar(myVar) {
        alert(myVar);
      }    
    </script>
  </head>
<body>
  <iframe id="ifr" src="iframe.html" border="1" ></iframe>
</body>
</html>


-------------

iframe file:


<html>
<head></head>
<body>
  <script type="text/javascript">
    var m=25;
    parent.getIFVar(m);
  </script>
</body>
</html>
0
 

Author Comment

by:APRESTOUS
ID: 19633734
Unfortunately the iframe file is generated authomatically and I can't change it.  ( I can but the next update will overwrite my changes ). So I have to find a way ( if there is one ) to get variable from the main file without touching iframe source.
0
 
LVL 28

Expert Comment

by:TName
ID: 19633770
Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
      function getIFVar() {
          var ifvar=window.frames.ifr.m;
          alert(ifvar);
      }    
</script>
</head>
<body onLoad="getIFVar();">
  <iframe id="ifr" name="ifr" src="iframe.html" border="1"></iframe>
</body>
</html>



The iframe is simply (but it shouldn't matter, as long as m is declared):

<html>
<head>
<script type="text/javascript">
    var m=25;    
  </script>
</head>
<body>
</body>
</html>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:third
ID: 19633927
is the framed page located on the same domain with the parent page? if not then you will encounter security issue.
0
 

Author Comment

by:APRESTOUS
ID: 19633964
Which issue?

In test stage I'd use different domains ( localhost and webserver) . In production - same (webserver).  
0
 
LVL 30

Accepted Solution

by:
third earned 1500 total points
ID: 19634007
if they will have same domain and webserver on production, then you will not encounter any problem.

use,

<script type="text/javascript">
function GetValue() {
  var ifvar=window.frames['mainwindow'].m;
  alert(ifvar);
}
</script>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
This article discusses how to implement server side field validation and display customized error messages to the client.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question