iframe and javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iuniamadalina
    New Member
    • Nov 2008
    • 3

    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.devittwiner y.com/our_wines.html" ></iframe>
    <form>
    <input type="text" name="textbox1" value="" />
    <input onclick="copyit (this.form.text box1)" type="button" value="Copy" name="btnCopy" />
    </form>
    [/HTML]....

    and the javascript function is:
    Code:
    function copyit(theField) {
    var selectedText = document.selection;
    if (selectedText.type == 'Text') {
    var newRange = selectedText.createRange();
    theField.focus();
    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.select ion;" i put:

    Code:
    iframe = document.getElementById("iframe1");
    var selectText =iframe.contentWindow.selection;
    and i get a big error...

    Can anyoane help me?... pls Thanks
    Last edited by acoder; Nov 15 '08, 06:58 PM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Either use:
    Code:
    window.frames["iframe1"].document...
    or where supported, contentDocument :
    Code:
    document.getElementById("iframe1").contentDocument
    or contentWindow.d ocument 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.

    Comment

    • iuniamadalina
      New Member
      • Nov 2008
      • 3

      #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=do cument.getEleme ntById('ifrmChi ld').contentWin dow.getSelectio n();
      document.all("t xtIndex").value = selectedText;

      }
      </script>
      <body>


      <input type="text" value="aaa" id="txtIndex"/>
      <input type="button" onClick="Settex t();" value="Add text"/>
      <br/>
      <iframe src="subindex.h tml" 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
      Last edited by acoder; Nov 18 '08, 11:05 AM. Reason: Added [code] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.

        Comment

        • iuniamadalina
          New Member
          • Nov 2008
          • 3

          #5
          That article realy helped. Thank you so much

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You're welcome :)

            Comment

            Working...