1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    You can try using jQuery which has an amazing selector functionality.
     
  3. SaswatPadhi

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

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Thanx pradeep :)
     

Share This Page