1

I have a main html page which has a text input field and iframe in it .

I would like to be able to click a link in the iframe and and have the input text box value change to the value of a variable called selectedText.

Im not sure how to get it to work . I have been able to get it to work only from the main page by using this :

newListNote.value = selectedText;
Jess
  • 133
  • 3
  • 12
  • Check this older answer http://stackoverflow.com/questions/17950598/using-iframe-with-local-files-in-chrome – Ramkumar Mar 30 '15 at 09:57

4 Answers4

0

I think this one is related to Same-Origin Policy and Cross Site Script policy. You can't achieve it (correct me if i'm wrong).

Gereltod
  • 2,043
  • 8
  • 25
  • 39
  • You cant if your running localy . If your running on a server or in node-webkit you can – Jess Mar 30 '15 at 09:35
0

As you cannot set the textbox value of parent page from iframe directly, create a javascript function in your main page like this,

function setValue(val) {
  document.getElementById('newListNote').value = val;
}

And call this function from the iframe page like this,

parent.setValue(selectedText);

I consider your textbox has id newListNote. Hope it works, thanks.

BabyDuck
  • 1,249
  • 1
  • 9
  • 22
0

Assuming your using jQuery as you tagged it and the iframe is another page within your site, it would be better to use .load()

$( "#result" ).load( "ajax/test.html" );

This then loads the page into your document where you can access the links. Then you can achieve your original goal. If the page is external to your site i'm not sure that this would work.

Good luck

ed-wright
  • 73
  • 1
  • 12
0

A combination of the two answers here by @balaG and @Gereltod are correct.

As @Gereltod suggested, you cannot run JavaScript within an iframe from outside the iframe. If you could, then there would be serious security problems with the internet!

As @BalaG suggested, if you want the website to change based upon an event within the iframe, you need to write code within the iframe, and prepend it with parent.

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64