JavaScript: How to access an image in main doc from iframe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • armanic
    New Member
    • Mar 2008
    • 4

    JavaScript: How to access an image in main doc from iframe

    Please help on the following:

    I have a document loaded in an IFRAME. In that document, I need in a JavaScript to access an image (having an ID) in the main document (the document including the IFRAME). What I want to do is to change the image source in the main document.

    My question: what is the (DOM) code to access that image. Is the .referrer used here?

    Thanks in advance, Arman
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    try:

    [CODE=javascript]window.frames['your_frame_nam e'].document.getEl ementById('img_ id')[/CODE]
    kind regards

    Comment

    • armanic
      New Member
      • Mar 2008
      • 4

      #3
      Sorry, it's doesn't work.

      I need in the IFRAME document the image in the parent. Thus, this refers to an image in the frame (at least, that's what I understand):
      Code:
      window.frames['your_frame_name'].document.getElementById('img_id').src
      This also doesn't work:
      Code:
      window.parent.document.getElementById('img_id').src
      Please help once more and explain why the above example is wrong.

      Thanks, Arman

      Comment

      • CpVermont
        New Member
        • Feb 2008
        • 14

        #4
        Originally posted by armanic
        Sorry, it's doesn't work.

        I need in the IFRAME document the image in the parent. Thus, this refers to an image in the frame (at least, that's what I understand):
        Code:
        window.frames['your_frame_name'].document.getElementById('img_id').src
        This also doesn't work:
        Code:
        window.parent.document.getElementById('img_id').src
        Please help once more and explain why the above example is wrong.

        Thanks, Arman
        I can't tell what exactly what you are trying to do here, but this is how I'd do it.

        Code:
        window.frames['frame_name'].document.getElementById("the_id").src = parent.document.getElementById("the_image_id").src;
        IE has given me trouble with that in the past so I had to do it like this.

        Code:
        var my_iframe = window.frames['frame_name'];
        var the_src = parent.document.getElementById("image_id").src;
        my_iframe.document.getElementById("the_iframe_image_id").src = the_src;
        I hope this helps if not maybe I'm not understanding what you are trying to do. Good day,
        CpVermont

        Comment

        • armanic
          New Member
          • Mar 2008
          • 4

          #5
          Let me explain what I'm trying to do:

          There is a document ("doc1") that contains an image ("img1") and an IFRAME ("iframe1") . In this IFRAME, a second document is loaded ("doc2").
          In doc2, I want to change the source of img1 in doc1.

          When doing this in doc1, the code will be:
          Code:
          function changeSrc()
            {
            document.getElementById("img1").src="any_image.gif"
            }
          The question is how this code must be when it is located in doc2.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            sorry for the misunderstandin g ... it should work with:

            [CODE=javascript]parent.document .getElementById ('img1').src = 'any_image.gif' ;[/CODE]
            assuming that the iframe is part of the doc1?

            kind regards

            Comment

            • armanic
              New Member
              • Mar 2008
              • 4

              #7
              Yes! This works.

              Thank you for your help.
              Arman

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                glad to hear that :) ... post back to the forum anytime you have more questions ...

                kind regards

                Comment

                Working...