473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

iframe and javascript

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
5 2259
acoder
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
That article realy helped. Thank you so much
Nov 18 '08 #5
acoder
16,027 Expert Mod 8TB
You're welcome :)
Nov 18 '08 #6

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

Similar topics

5
by: Mel | last post by:
i need to have 2 side by side iframes, a link on top of the one will show/hide the other can someone help me pleeeeezzzzz ?
4
by: christine.nguyen | last post by:
Hello, Here's the deal. I have an html page that contains an iframe. This iframe has an id that is unknown to me. This iframe also has test.html as it's source. Within test.html runs some...
3
by: Timo | last post by:
In javascript code on MyPage.aspx, I set a hidden IFRAME's source url: myframe.location.href = 'someotherpage.aspx'; If the session has timed out, preventing someotherpage.aspx from being...
13
by: ukrbend | last post by:
I'm new to Javascript and to html and am trying to make the following code snippet work but it doesn't. It refuses to call the getPage() function and I always get a 404 error. I know the code is...
6
by: paul | last post by:
HI! How do we send a variable from an Iframe page back to its parent? I have a script that calculates the iframe's window size but I need to know how to send that value back to its parent so I...
7
by: ukrbend | last post by:
I use an iframe on my home page and everything works perfectly. But now I decided to add a popups to my page. The popups come not from within the iframe but from the parent frame. Again, everything...
18
by: Chris Ianson | last post by:
Hi geniuses (or is that genii, or genies) The challenge is as above really. I have a page with an iframe in it, and need to call a JS function in the *parent* page, *from* inside the iframe. ...
3
by: PCgeek | last post by:
sorry moved this over to javascript forum, didn't mean to post 2x! Hi guys, I'm trying to put the finishing touches on my website and could really use some help on this particular issue. My page...
5
by: brad | last post by:
I'll post my code below. My goal is to grab the selected text in an iFrame from javascript (really any way possible). When I highlight text in the iFrame and click the link outside of the iFrame, I...
1
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.