1

I'm trying to change the value of a text box within iframe. I have tried using GetElementById in every way i could find and nothing seems to work.

I found a alternative to iframe by using the Object data tag but it has the same problem.

My code more or less, I changed it a bit for presentation:

 <html>
 <head>

 <title>None</title>

 </head>
 <body>

 <script type="text/javascript">

 function changeValue() {
 var textBox = document.getElementById('userName');
 textBox = "hello!";

 }

 </script>

 <iframe id="myFrame" src="http://www.website.com"></iframe> 

 <input type="button" onclick="changeValue()" value="Submit">

 </body>
 </html>
1
  • you can't access a page in an iframe if it is on another domain. This is due to security reasons Commented Jan 17, 2013 at 12:52

2 Answers 2

4

This is not possible for security reasons.

If you had access, you would be able to load, say facebook.com in an iframe on your website and extract user details with JavaScript.

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

4 Comments

Very true, but there is nothing in the OP's post that suggests that the page in the iframe is not on the same domain.
Of course, but since he used http://website.com as url, I imagined it would be an external site.
Thanks for you're answer. I was trying to access an external domain. I tested my code on my domain and it worked. Sadly this means i have hit a brick wall in my project but at least no more blind alleys. Out of curiosity is the problem on the external domain or in javascript itself?
It's a limitation in the browser.
1

Try something along the lines of

document
    .getElementById('myFrame')
    .contentWindow
    .document
    .getElementById('userName')
    .value='hello';

As the others pointed out, this will only work if the page inside the iframe is on the same domain.

Comments

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.