How do I get source code from a webpage?
Thanks to:
- @PLB
- @Shadow Wizard
- Getting the source code of an iframe
- http://www.frihost.com/forums/vt-32602.html
- @Matt Coughlin.
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);
});
When I view the source code of a potentially malicious page, can the website harm my computer?
The general answer is to just disable javascript and cookies in your browser first.
Generally yes it's ok to view source, especially if javscript is disabled prior.
You can if their scripts are readable, many sites however will minimize the code, which is generally not very readable.
If javascript is disabled it's likely that their tracking would not work or at best be incomplete.
How do I get the HTML source from the page?
Use
document.documentElement.outerHTML
or
document.documentElement.innerHTML
How to I find JavaScript source in web page?
In firefox open the desired page, press F12 to open the developer tools.
In the "debugger" tab press ctrl + shift + F (Search in files) and search for "jspaginate". It will present you all the files containing that word.
Firefox Developer Tools - Search
Firefox Developer Tools: search for files with the debugger (Video)
Or with FireBug that is no longer supported in Firefox >57*
Install firebug, activate it, activate the script panel too and then look for the jspaginate object. In a js file you will find it:
var jspaginate = {
data:{},
loading: false,
init: function(action, last){
var view = this,
target, current;
if(this.loading !== true){
view.loadingSequence();
if(action === 'first'){
target = 0;
view.update(target, 0);
}else if(action === 'prev'){
current = parseInt(view.data.pageIndex)-1;
target = (current)*view.data.range;
view.update(target, current);
}else if(action === 'next'){
current = parseInt(view.data.pageIndex)+1;
target = (current)*view.data.range;
view.update(target, current);
}else if(action === 'last'){
current = parseInt(last)-1;
target = (current)*view.data.range;
view.update(target, current);
}
}
},
update: function(target, current){
this.data.pageIndex = current;
this.pushState(target, current);
this.getData(target);
},
pushState: function(target, current){
var state = { 'page_id': current, 'user_id': 2 },
title = 'Page'+ current,
url = '?start='+target+'&tstart=0';
history.pushState(state, title, url);
},
loadingSequence: function(){
this.loading = true;
$j('.j-pagination').append('<div class="j-loading-big"><span></span></div>');
$j('.all-replies-container').css('opacity','.5');
},
removeLoading: function(){
$j('.j-loading-big').remove();
$j('.all-replies-container').css('opacity','1');
this.loading = false;
},
updateUI: function(data){
$j('.all-replies-container').html(data);
$j('html, body').animate({
scrollTop: ($j(".all-replies-container").offset().top -180)
}, 800);
this.removeLoading();
},
getData: function(target){
var view = this,
tId = (this.data.threadId).split('/')[2],
urlString = jive.app.url({path:'/inline-thread.jspa?thread='+tId+'&start='+target+'&tstart=0'});
$j.ajax({
url: urlString,
cache: true,
async: true,
type:'POST',
dataType : 'html'
}).success(function(data) {
view.updateUI(data);
}).error(function(data) {
console.log(data);
});
}
}
;
Related Topics
How to Prevent Buttons from Submitting Forms
Why Are Inline Event Handler Attributes a Bad Idea in Modern Semantic Html
In What Situations Would Ajax Long/Short Polling Be Preferred Over Html5 Websockets
Using Html5/Canvas/JavaScript to Take In-Browser Screenshots
Unescape HTML Entities in JavaScript
How to Prevent Form from Being Submitted
How to Send Formdata Objects With Ajax-Requests in Jquery
Jquery/JavaScript to Replace Broken Images
How to Append to Innerhtml Without Destroying Descendants' Event Listeners
Why Split the ≪Script≫ Tag When Writing It With Document.Write()
How to Load Up CSS Files Using JavaScript
Get Cursor Position (In Characters) Within a Text Input Field
What Browsers Support Html5 Websocket API
Replacing CSS File on the Fly (And Apply the New Style to the Page)