PDA

View Full Version : Parsing AJAX Response HTML



mes-x
9 Feb 2012, 11:13 AM
Hello everyone!

Is it in ExtJS possible to parse an HTML AJAX Response?

I'm downloading some HTML pages with Ext.Ajax.request(). The pages contain full html with doctype head and body elements. Now i want to parse it in any way (reading some header tags, modify some body stuff, ...).

As i know, jQuery could do that by using $(responseVariable.responseText) but can do ExtJS something similar?


thank you very much for all your respones and best regards
Manuel

friend
9 Feb 2012, 12:39 PM
Here's a code sample from the API docs for Ext.Ajax.request(). Note that you can get at the response and do anything you want with it:




Ext.Ajax.request({
url: 'page.php',
params: {
id: 1
},
success: function(response){
var text = response.responseText;
// process server response here
}
});

mes-x
9 Feb 2012, 1:39 PM
Thank you very much for your quick response!

The question is about processing the server response which is a full html page. I want to parse the html (read it (for example Ext.Element.getAttribute()), manipulate it (example Ext.Element.createChild())) and after that maybe (depending on read) write it to the DOM.

The reason why not just creating a hidden <div> and adding the responseText there is simply because of the <head>. I need to also parse all <meta> stuff or <title> tags which are not placed inside the DOM. Or <script> tags would execute immediately but i just want to read the src attribute from it, ..., ..., ..., ...

best regards!
Manuel

friend
10 Feb 2012, 9:59 AM
The response is basically just a big String and there are no DOM objects associated with it. You'll have to do the work of parsing/creating DOM objects (possibly via Ext.DomHelper?).

You might be better off rendering the HTML response to a panel that's hidden or offscreen, manipulating the DOM appropriately, then showing the panel.

sskow200
10 Feb 2012, 11:26 AM
Couldn't you just load the page into an iframe and manipulate it there?

friend
10 Feb 2012, 12:52 PM
I think the browser prevents you from being able to manipulate DOM and/or perform scripting on the contents of an iframe.

sskow200
10 Feb 2012, 12:57 PM
I have used a scenario where I needed to display a loadmask while an iframe loads. It is possible to inject an onload="" event into the element itself. So, in theory, you could create a custom function to give you back data (ie: the body of the DOM) when the iframe finishes loading.

mes-x
15 Feb 2012, 6:00 AM
Ok, so there is no elegant way of parsing the responseText as HTML?
I think, i'll use jQuery with ExtJS.

thx for all your help!