155
rated 0 times
[
157]
[
2]
/ answers: 1 / hits: 24614
/ 12 Years ago, thu, june 7, 2012, 12:00:00
How can we get the source code of a webpage from a webpage in php and/or javascript?
More From » php
How can we get the source code of a webpage from a webpage in php and/or javascript?
Thanks to:
First, you must know that you will never be able to get the source code of a page that is not on the same domain as your page in javascript. (See http://en.wikipedia.org/wiki/Same_origin_policy).
file_get_contents($theUrl);
Firstly, by XMLHttpRequest : http://jsfiddle.net/635YY/1/
var url=../635YY,xmlhttp;//Remember, same domain
if(XMLHttpRequest in window)xmlhttp=new XMLHttpRequest();
if(ActiveXObject in window)xmlhttp=new ActiveXObject(Msxml2.XMLHTTP);
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)alert(xmlhttp.responseText);
};
xmlhttp.send(null);
Secondly, by iFrames : http://jsfiddle.net/XYjuX/1/
var url=../XYjuX;//Remember, same domain
var iframe=document.createElement(iframe);
iframe.onload=function()
{
alert(iframe.contentWindow.document.body.innerHTML);
}
iframe.src=url;
iframe.style.display=none;
document.body.appendChild(iframe);
Thirdly, by jQuery : http://jsfiddle.net/edggD/2/
$.get('../edggD',function(data)//Remember, same domain
{
alert(data);
});