Obtaining links from an iframe with javascript

Discussion in 'JavaScript' started by hip_hop_x, Feb 11, 2009.

  1. #1
    I am not able to obtain the links inside an iframe and i don't know why. Maybe you can tell me a solution (i want to do this in javascript).

    
    <script>
    function fget(){
    var links = self.frames['rame'].document.links
    	for(var i = 0; i < links.length; i++) {
    		  var linkHref = document.createTextNode(links[i].href);
    		  document.getElementById("rezult").innerHTML.=linkHref;
    	}
    }
    </script>
    
    <span onclick="fget();">Click for info</span>
    <iframe src='http://somesite.with_a_tags.com' width='728' height='=90' name='rame'></iframe>
    <div id="rezult"></div>
    
    Code (markup):
     
    hip_hop_x, Feb 11, 2009 IP
  2. FotoZone

    FotoZone Well-Known Member

    Messages:
    363
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    145
    #2
    FotoZone, Feb 11, 2009 IP
  3. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    huh are you spamming ?
     
    hip_hop_x, Feb 11, 2009 IP
  4. meetuchoudhary

    meetuchoudhary Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    does your code is working fine without iframe if yes then you may try to write this script in the head section of the page which is loading in the iframe probably the problem is that the page is not getting the script function

    or please provide the error details you are getting
     
    meetuchoudhary, Feb 11, 2009 IP
  5. mortgageloan

    mortgageloan Peon

    Messages:
    339
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes that can be done...script may nt be getting loaded...put it in the head tag of the page..
     
    mortgageloan, Feb 12, 2009 IP
  6. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #6
    i tried that already and didn't work :(
     
    hip_hop_x, Feb 12, 2009 IP
  7. meetuchoudhary

    meetuchoudhary Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    if it didn't worked that means it should be giving you some JavaScript errors can you trace those and tell us what those error description is.. or if you don't know who to traces the error tell me i may help you
     
    meetuchoudhary, Feb 12, 2009 IP
  8. dman_2007

    dman_2007 Peon

    Messages:
    1,259
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #8
    + is used as string concatenation operator in javascript not . (dot) which you seem to be using in this line : document.getElementById("rezult").innerHTML.=linkHref;

    Also, does the the parent page and the iframe originate from the same domain?
     
    dman_2007, Feb 12, 2009 IP
  9. meetuchoudhary

    meetuchoudhary Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i even don't think that concatenation is necessary in this case what say
     
    meetuchoudhary, Feb 12, 2009 IP
  10. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #10
    the iframe loads an external page, what i want to do with javascript is to get all the links from the iframe, grabbing the links into an array.
     
    hip_hop_x, Feb 15, 2009 IP
  11. meetuchoudhary

    meetuchoudhary Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    the External link which the iframe is opening is that frame is designed by you or not?
     
    meetuchoudhary, Feb 15, 2009 IP
  12. dman_2007

    dman_2007 Peon

    Messages:
    1,259
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Then it can't be done in javascript, due to javascript security policy. Javascript scripts are not allowed access to webpage in different frame which come from different domain.
     
    dman_2007, Feb 18, 2009 IP
  13. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #13
    it's an external link from other website not mine, ah i didn't know that :(
    I'll just have to do this with php then.
     
    hip_hop_x, Feb 19, 2009 IP