Converting GET HTML response to document

I am developing a page that reads the source of another page and I need to extract certain information from that page. I currently have a project that is pushing a live source with data, but I can't figure out in my entire life how to convert that string to a document.

My rationale for using the doc is that I need to use getElementById

etc. to get the value of those elements.

What have I tried?

  • Assigning HTML to an invisible div on my page. This kind of works, although it doesn't render the entire HTML line and provides a "shorter" execution of that page.

  • Find substrings manually. As you can imagine, this is a crappy way of doing things and gives very unreliable results.

  • DOM parser to transform the document and then request it, but that fails.

Any help whatsoever would be seriously appreciated.

relevant code:

$.ajax({
  method: "GET",
  dataType: '',
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  },
  success: function(res) {
    //shows the entire source just fine.
    console.log("Value of RES: " + res);
    bootbox.hideAll();
    //shows a "truncated" copy of the source
    alert(res);
    $("#hiddendiv").html(x);
    var name = document.findElementById("myitem");
    alert(name);
  },

      

+3


source to share


2 answers


Create a hidden IFRAME on your document. Then set the content of that IFRAME to the HTML you want to request. Route this IFRAME to your javascript when you execute your request. See How can I access iframe elements using Javascript? to see how to do this.



0


source


Another (probably better) option is to use jQuery. jQuery allows you to create, manipulate, and query HTML files in memory. Query for DOM elements in jQuery is even easier than in pure javascript. See: http://jquery.com/ .



//Get a jQuery object representing your HTML
var $html = $( "<div><span id='label'></span></div>" );

//Query against it
var $label = $html.find( "#label" ); //finds the span with and id of 'label'

      

-2


source