0

I'm making a request to an external url using javascript and I get a response, which is in a text form. I want to retrieve an element with some id (e.g "id1") from the response (without jQuery) and in general execute some javascript code on this html response. This looks so:

var xmlHttp = new XMLHttpRequest()
xmlHttp.open( "GET", "some url", false )
xmlHttp.send( null )
var mainpage = xmlHttp.response

Basically I want to get a content of a div with id=id1 in mainpage, retrieve some of his childs and some other basic javascript operations.

Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48
  • Please see http://stackoverflow.com/a/16825593/1348195 – Benjamin Gruenbaum May 17 '14 at 14:01
  • The given question does not target the thing I wanted to achieve. I don't have problems with getting the response: the content of the variable mainpage is a valid html response. The problem is that I want to retrieve some content from that response in the current page using the standard javascript functions. – Ivaylo Toskov May 17 '14 at 15:28
  • 1
    You mean like `var el = document.createElement("div"); el.innerHTML = responseText; var div = el.querySelector("#id1"); console.log(div.innerHTML);` ? – Benjamin Gruenbaum May 17 '14 at 15:30
  • Yes, this is what I meant. Is it possible to implement that without using the character "#" ? – Ivaylo Toskov May 17 '14 at 15:38
  • 1
    Yes, `el.querySelector("[id=id1]")`, I'm probably going to regret this, but why? – Benjamin Gruenbaum May 17 '14 at 15:39
  • I'm doing a XSS related homework for the university (don't worry, nothing harmful will be done to the real world, as you can see I am not even a script dummy). I found the vulnerable place where to inject the code, but some characters like "#" and ";" are breaking the injection, therefore I need to avoid them. This helped me anyways, thanks! Is there a way to get the content of all of the childs of this div with id=id1? – Ivaylo Toskov May 17 '14 at 15:49
  • Sure, a query selector `[].map.call(el.querySelector("#id1 a ")).map(function(el){ return el.textContent; }).join(" ");` , if you have any more small questions - try the chat, this is clogging the question comments area. – Benjamin Gruenbaum May 17 '14 at 15:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/53882/discussion-between-ivaylo-toskov-and-benjamin-gruenbaum) – Ivaylo Toskov May 17 '14 at 15:55

0 Answers0