Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript - Get links from webpage

  • 26-01-2011 5:46pm
    #1
    Closed Accounts Posts: 2,696 ✭✭✭


    I am trying to obtain a list of links on a webpage. I have the below code which outputs only one link

    I can obtain each link by changing elms[0] however i cannot loop to obtain them all

    Any assistance is welcome
     
    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","[URL]http://www.comp.dit.ie",false[/URL]);
    xmlhttp.send();
    
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    //txt=document.getElementById("footer").innerHTML;
    //document.write("<p>The text from the intro paragraph: " + txt + "</p>");
     
    var elms = document.getElementsByTagName("a");
    //for (int i=0; i<elems.length; i++){
    //document.write("<p>The text from the intro paragraph: " + elms[i] + "</p>");
    //document.write("<p>The text from the intro paragraph: " + elms.length + "</p>");
    document.write("<p>The text from the intro paragraph: " + elms[0] + "</p>");
    document.write("<p>The text from the intro paragraph: " + elms[1] + "</p>");
    document.write("<p>The text from the intro paragraph: " + elms[2] + "</p>");
    
    }
    </script>
    </head>
    <body>
    <div id="myDiv"><h2>Load Page Selection Data</h2></div>
    <button type="button" onclick="loadXMLDoc()">Load Data</button>
    </body>
    </html>
     
    


Comments

Advertisement