How to parse HTML from XMLHttpRequest in Javascript

Discussion in 'JavaScript and AJAX' started by SaswatPadhi, Aug 27, 2009.

  1. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Hi all,

    I hope JS experts can help me with this one.

    I accept a web-page(HTML) using XMLHttpRequest. Then, I want to parse the HTML, so that I can access the DOM tree of the page.
    Can anyone help me with this ?

    I have tried doing it like :

    Code:
    var data = XHR.responseText;
    var myDiv = document.createElement("div");
    
    myDiv.innerHTML = data.replace(/<script(.|\s)*?\/script>/g, '');
    
    Then I try doing things like :
    Code:
    myDiv.getElementsByTagName("table");
    
    but they fail. :(

    Thanx

    Saswat
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You can try using jQuery which has an amazing selector functionality.
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Thanx pradeep :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice