3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by am5a03
0

It depends on the html tag displaying the text and its id or name for example lets say you got an text response which is HTML and the tag displaying this text you want to modify is a DIV tag with id "message" you can work with it like this.

// assuming the XMLHttpRequest is xhr
var responsestr=xhr.responseText;
// you have to include the gotten response on the page first, lets say a hidden div with the id "hidden"

document.getElementById("hidden").innerHtml=responsestr;
var wantedstr;

wantedstr=document.getElementById("message").innerHtml;
//then manipulate the text the way you want it

its kind of shady though but I hope it helps

0
<html>
	<title>AJAX return text</title>
	<head>
		<script type = "text/javascript">
			function getXMLHTTPRequest(){
				var req = false;
				try {
					req = new XMLHttpRequest();
				}catch(err1){
					try{
						req = new ActiveXObject("Msxml2.XMLHTTP");
					}catch(err2){
						try{
							req = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(err3){
							req = false;
						}
					}
				}
				return req;
			}
			
			var http = getXMLHTTPRequest();
			
			function getServerText(){
				var myurl = "http://www.weather.com/weather/today/Hong+Kong+China+CHXX0049";
				http.open("GET", myurl, true);
				http.onreadystatechange = useHttpResponse;
				http.send(null);				
			}
			
			function useHttpResponse(){
				if( http.readyState == 4){
					var mytext = http.responseText;
					alert(mytext);
				}else{
					document.getElementById('test').innerHTML = 'Not fetched';
				}
			}
			
		</script>
	</head>
	<body onload="getServerText()">
		<div id='test'></div>		
	</body>
</html>>

thx meffe!

This is what I have, but the alert box just give me some blank msg.
It gives me "null" if I replace http.responseText with http.responseXML
What's wrong with my coding?

Edited by am5a03: n/a

0

nothing is wrong with your code. responseXML might give you null when the returned value from request is not valid XML.

it is better if you create an element in body with style="display: none" attribute then do the parsing from there as meffe suggested above.

0

I've written a simple html page called 'test.html' and it works!

<html>
	<head>
		<title>HelloWorld</title>
	</head>
	<body>
		<div id='hello'>Hello</div>		
	</body>		
</html>

All the html codes displayed correctly.
I just wondering why I cannot do this when I linked to some other sites outside.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.