I am trying to get the source code of html by using a xmlhttprequest with a url. Is there anyone who can help me with this? I am new to programming and I am not too sure how can I do it without jQuery. Thanks in advance.

share|improve this question
1  
You may want to look into the problem of the same origin policy...Just search on SO and you will find tons of info. – josh.trow Jun 16 '11 at 16:44
but is there any other way of going about this thing? like not using xmlhttprequest? with just javascript? – simplified Jun 16 '11 at 16:47
1  
no. xmlhttprequest and iframes are the only way, and both are limited by same-origin policy. If you want to get around this, the remote server needs to cooperate (by serving as jsonp, or putting a special header on the data it serves) – rob Jun 16 '11 at 17:28

2 Answers

use jQuery

$.ajax({ url: 'your-url', success: function(data) { alert(data); } });

data is your html

without jQuery

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}
var request = makeHttpObject();
request.open("GET", "your_url", true);
request.send(null);
request.onreadystatechange = function() {
  if (request.readyState == 4)
    alert(request.responseText);
};
share|improve this answer
@Senad Meskin thanks for your answer, but issit possible to do it with jQuery? i was wondering if there are other methods to do it. – simplified Jun 16 '11 at 16:49
@Senad Meskin thanks. i was trying to write this function in a empty html file that just put the code above in the script tag, however. it seems that the readystate is 1 and didn't get through. do you know why? i have changed to url to already. but it still doesn't work. – simplified Jun 16 '11 at 17:50
Does your url points to another server, if so that is the reason, security issue. – Senad Meškin Jun 16 '11 at 17:53
@Senad Meskin lets say it's google.com or youtube.com? is it possible? – simplified Jun 16 '11 at 17:54
No its not possible, only thing that you can is call your url, and on serverside code call www.google.com and write to response content of google.com – Senad Meškin Jun 16 '11 at 18:06
show 1 more comment

There is a tutorial on how to use ajax here: http://www.w3schools.com/ajax/default.asp

This is an example code taken from that tutorial:

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.