Get variable from iframe

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 :(  
APRESTOUSAsked:
Who is Participating?
 
thirdConnect With a Mentor Commented:
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
 
TNameCommented:
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
 
APRESTOUSAuthor Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
TNameCommented:
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
 
thirdCommented:
is the framed page located on the same domain with the parent page? if not then you will encounter security issue.
0
 
APRESTOUSAuthor Commented:
Which issue?

In test stage I'd use different domains ( localhost and webserver) . In production - same (webserver).  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.