0
request.open("GET", selectedTab + ".html", true);

Will this respond me back with the whole html page or just the text part in some specific tags. If my html page has images and paragraphs, what exactly will be retured to me in my request object, and how can i use individual tags(using javacript), if it returns them all?

For example, say my html file is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> 
<link type="text/css" href="something" rel="stylesheet">
</head>
<body>
<p>hello</p>
<img src="something.jpg" />
</body>
</html>

now how do i get the individual coponents of the file.

Jim Deville
10.7k1 gold badge42 silver badges49 bronze badges
asked Jul 8, 2011 at 5:13

2 Answers 2

1

You will get just the HTML code of the file. You'll have to parse the elements yourself to pull any external files (i.e. css, js, etc)

Edited to respond to edits (x2): Your ajax request will get the HTML, but if you wanted to get the stylesheet, or picture, you would have to parse it out. For example (off the top of my head, not fully tested :)):

var iframe = document.createElement("iframe"); //creating dummy iframe to load html into in order to parse it. DOMParser is another option;
iframe.innerHTML = response; //assuming response is the variable you stored the responseText from the AJAX request
var doc = iframe.contentDocument //get the document property of the iframe
var url = doc.getElementsByTagName("link")[0].href; //gets url of stylesheet
var img = doc.getElementsByTagName("img")[0].src; //gets url of img

You could then make more requests to get the actual items.

If all you want is to get the various elements, then you could use the doc.getElementsByTagName("p") technique to get tags. Or there are other DOM methods that may work better depending on the HTML and context.

answered Jul 8, 2011 at 5:15
Sign up to request clarification or add additional context in comments.

2 Comments

still dont get it, normally if i request an object named request,and send it to the php file, and then i want some textual response from the php, i echo it in there, and get the text in my current file by request.responseText, now in your reply, what is iframe? and is your response equivalent to my request object? and what does contentDocument does?
added more comments to try to clarify
0

I'd recommend using the http://jquery.com/ library to help parse the returned data. For example:

<script type="text/javascript">
$(function() { // just a document.ready wrapper
 $.get(selectedTab + ".html", function(data) {
 var parsedData = $(data);
 var specificValue = $("#someID", parsedData); // any CSS selector will work in place of #someID
 });
});
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
answered Jul 8, 2011 at 5:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.