iframe and javascript

  • iunia_madalina
  • Born
  • Born
  • iunia_madalina
  • Posts: 2

Post 3+ Months Ago

Hi! i have a html page with a iframe. I want to be able to select some text from iframe and when i push a button outside the iframe the selected tet shoul be copied in a text field ( the text field is also outside the iframe). I searh for days how to cummunicate between the iframe and parrent - and nothing. I already have a function in javascript wich copies the selected text in a text field (the text field is outside the iframes).

This is the code....
<p>Some text outside the iframe.</p>
<iframe name="iframe1" id="iframe1" src="http://www.devittwinery.com/our_wines.html"></iframe>
<input type="text" name="textbox1" value="" />
<input onclick="copyit(this.form.textbox1)" type="button" value="Copy" name="btnCopy" />

and the javascript function is:
function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');

this is the function that works for a text outside the iframeeas. when i want to copy the text from the iframe instead of "var selectedText = document.selection;" i put:

iframe = document.getElementById("iframe1");
var selectText =iframe.contentWindow.selection;

and i have a big error...

Can anyoane help me?... please Thanks
  • iunia_madalina
  • Born
  • Born
  • iunia_madalina
  • Posts: 2

Post 3+ Months Ago

I found a solution for my probleme but it works only for pages from my domain.

this is the code:


function Settext()

var selectedText=document.getElementById('ifrmChild').contentWindow.getSelection();
document.all("txtIndex").value = selectedText;


<input type="text" value="aaa" id="txtIndex"/>
<input type="button" onClick="Settext();" value="add text"/>
<iframe src="subindex.html" id="ifrmChild">

But I want the page from iframe to be from defrent domain.

Is there a solution for my probleme?
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I think security considerations should rule that out.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 27 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.