By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,961 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,961 IT Pros & Developers. It's quick & easy.

iframe and javascript

P: 3
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....
....
[HTML]<p>Some text outside the iframe.</p>
<iframe name="iframe1" id="iframe1" src="http://www.devittwinery.com/our_wines.html"></iframe>
<form>
<input type="text" name="textbox1" value="" />
<input onclick="copyit(this.form.textbox1)" type="button" value="Copy" name="btnCopy" />
</form>
[/HTML]....

and the javascript function is:
Expand|Select|Wrap|Line Numbers
  1. function copyit(theField) {
  2. var selectedText = document.selection;
  3. if (selectedText.type == 'Text') {
  4. var newRange = selectedText.createRange();
  5. theField.focus();
  6. theField.value = newRange.text;
  7. } else {
  8. alert('select a text in the page and then press this button');
  9. }
  10. }
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:

Expand|Select|Wrap|Line Numbers
  1. iframe = document.getElementById("iframe1");
  2. var selectText =iframe.contentWindow.selection;
  3.  
and i get a big error...

Can anyoane help me?... pls Thanks
Nov 15 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Either use:
Expand|Select|Wrap|Line Numbers
  1. window.frames["iframe1"].document...
or where supported, contentDocument:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("iframe1").contentDocument
or contentWindow.document if not supported. You could also move the function into the iframe page and call it instead.

Note that the iframe page must be from the same domain.
Nov 15 '08 #2

P: 3
Thank for your help. It realy helped me.

this is the code that works fine in firefox and with the iframe pages from my domain:

[HTML]<html>
<script>

function Settext()
{

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

}
</script>
<body>


<input type="text" value="aaa" id="txtIndex"/>
<input type="button" onClick="Settext();" value="Add text"/>
<br/>
<iframe src="subindex.html" id="ifrmChild">
</body>
</html>
[/HTML]



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

Is there a solution for my probleme?
Thanks
Nov 18 '08 #3

acoder
Expert Mod 15k+
P: 16,027
For a cross-browser selection code snippet, see this article.

For cross-domain iframe pages, you will need to serve the pages from your domain.
Nov 18 '08 #4

P: 3
That article realy helped. Thank you so much
Nov 18 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You're welcome :)
Nov 18 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.