1

I want to access variable from iframe without editing iframeContent.html page. I don't know why alert window still shows 'undefined'

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">
    var iframe0=0;
    var iframe0document=0;
    var inputIframe=0;      

    function getIframeText() {
        var iframe0 = document.getElementById("iframe123");
        var iframe0document=iframe0.contentDocument||iframe0.contentWindow.document;
        var inputIframe = iframe0document.getElementById("wynik2");
        alert(inputIframe.value);
    };
</script>
</head>

<body>

<div>
    <button onclick="getIframeText()">get iframe text</button>
    <iframe id="iframe123" src="iframeContent.html" >

</div>
</body>
</html>

iframeContent.html:

<html>
<head>
<title>IFrame Child Example</title>
<script type="text/javascript">

var asd="12";

</script>
</head>
<body>

<div id="wynik2"></div>
<script type="text/javascript">
document.getElementById("wynik2").innerHTML=asd;
</script>

</body>
</html>

Frame on parent page looks good (shows number 12). I'm testing my page on Chrome but through command window typing 'allow file access from files'. So this isn't problem. Global variables are also set (am I doing it right?) so I don't know why is still udefined.

2
  • I would really hope this is not possible because of security implications. Commented Sep 22, 2017 at 11:07
  • Thanks for comment, but is there any possibility to grab this value from iframe and put it in variable? (in other project i want to do some math equations with value from iframe) Commented Sep 22, 2017 at 11:27

1 Answer 1

1

use inputIframe.innerText instead of inputIframe.value . "wynik2" is a div, right? cheers! :)

Sign up to request clarification or add additional context in comments.

1 Comment

@iwona: you're welcome. dont forget to accept my answer. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.