Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  157] [ 2]  / answers: 1 / hits: 24612  / 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

 Answers
3

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).



In PHP, this is how you do it :



file_get_contents($theUrl);


In javascript, there is three ways :



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);
});

[#85091] Wednesday, June 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
;