2

I have a main.html, inside it is <ifram id='if' name='if'>, which loads another site (this loads a site not in my domain, like Facebook).

The site which is loaded in the iframe has <form name='form2' id='form2'>, and in it there's <input type='text' name='tb' id='tb'>

Also in main.html, I have a button and I was wondering if we could place some Javascript on the button so that when we click it, the text in textbox named tb changes to say 'az'.

How can we do this?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Adam Smith
  • 35
  • 1
  • 7

3 Answers3

2

You can' do that because that would be cross-site scripting, which is prohibited for security reasons. You could use a proxy, feed the HTML in the iframe through your own site so it's no longer cross-site from the browser's perspective.

If the <iframe> is from the same domain, the elements are easily accessible as:

$("#iFrame").contents().find("#someDiv")

Test: http://jsfiddle.net/Yy6tu/ press Ctrl+Shift+j and see what you got!


UPDATE: In case you have the a other.html file on your same folder, you can do this:

main.html:

<head>
    $('document').ready(function(){
        $("#if").contents().find("#b").val('lalalala');
    });
</head>

<body>
    <input type='button' name='executer' value="Maro">
    <iframe width="600" height="400" src='other.html' name='if' id="if"></iframe>
</body>

other.html:

<form action="x" name="form2" id="form2"> 
     <input type="text" name="tb" id="b">
</form>

That should work!

Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
1

That my friend is not possible. Cross-domain protection although limits some features but is of great advantage.

StackOverflow

This might help:

http://msdn.microsoft.com/en-us/library/bb735305.aspx

A possible solution would be to re-load the iframe. And add the text value as a param in iframe's url query string. But that would be a far-fetched solution.

Another possible solution would be to use an intermediary iframe or otherwise known as proxy iframe. This might help:

http://www.tomslabs.com/index.php/2012/06/iframes-and-javascript-cross-domain-security/

EDIT:

If iframe is on same domain as page than this should work.

$('#if').contents().find('#b').val('fd');
Community
  • 1
  • 1
Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41
  • This could also help: http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe – Tomas Ramirez Sarduy Feb 06 '13 at 07:24
  • Thanx for your alternative @Jehanzeb.Malik :) And can how can we get location/source_code of iframe from parent which loads site from different domain :)? – Adam Smith Feb 06 '13 at 07:28
  • 1
    @AdamSmith Read my lips carefully: Same origin policy. – Danny Beckett Feb 06 '13 at 07:36
  • @AdamSmith The solution that I was suggesting would only work if both the sites are developed by you. Then you can catch the url query string param and use it accordingly inside iframe. But as you mentioned that you would be opening other sites like facebook (which you dont have control of), this solution won't work. But if you are able to proxy the iframe you can make it work. But I will say only implement it if really necessary. – Jehanzeb.Malik Feb 06 '13 at 07:36
  • If you dont know how to proxy iframe this might help. http://www.tomslabs.com/index.php/2012/06/iframes-and-javascript-cross-domain-security/ – Jehanzeb.Malik Feb 06 '13 at 07:37
  • Okay let me check the link And yeah both site is developed by me & are in same domain,same folder now. – Adam Smith Feb 06 '13 at 07:45
  • 1
    Well you should have clearly mentioned that you are loading page from same domain. Loading facebook is not loading from same domain. If both pages are on same domain than this should suffice `$('#if').contents().find('#b').val('fd');` – Jehanzeb.Malik Feb 06 '13 at 08:00
  • 1
    @DannyBeckett : Okay maybe i should ask updated question as new question :) – Adam Smith Feb 06 '13 at 08:36
0

This is strictly not possible, since it directly violates same origin policy.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134