Click to See Complete Forum and Search --> : Get a page's source code with javascript


CJX
04-10-2006, 11:47 AM
Is there any way to get a page's source code with javascript?

ie. in a page with 3 frames, there is a frame with page 1, a frame running the javascript and a third page with a textarea

How can I get the source code of page one into the textarea of page 3?

Thanks in advance for any help :)

mrhoo
04-10-2006, 11:52 AM
var str= window.frames[0].document.documentElement.innerHTML;
var ta=window.frames[2].documentGetElementsByTagName('textarea')[0];
ta.value=str;

This only works if the frames are on the same domain

CJX
04-10-2006, 11:56 AM
Thanks, I'll give it a go

Ultimater
04-10-2006, 12:33 PM
var ta=window.frames[2].documentGetElementsByTagName('textarea')[0];
should be
var ta=window.frames[2].document.getElementsByTagName('textarea')[0];

The problem with innerHTML is that it will be reading at run-time and will most likely return a different source than what the actual source is between the document.writes, and user interaction.

I personally just throw either one of the following into the addressbar on ANY page I want the source to:

javascript:if(%22XMLHttpRequest%22 in window)xmlhttp=new XMLHttpRequest();if(%22ActiveXObject%22 in window)xmlhttp = new ActiveXObject(%22Msxml2.XMLHTTP%22);xmlhttp.open('GET', location.pathname,true);xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4) {alert(xmlhttp.responseText)}};xmlhttp.send(null);void(0);

or

javascript:if(%22XMLHttpRequest%22 in window)xmlhttp=new XMLHttpRequest();if(%22ActiveXObject%22 in window)xmlhttp = new ActiveXObject(%22Msxml2.XMLHTTP%22);xmlhttp.open('GET', location.pathname,true);xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState!=4)return;var k=xmlhttp.responseText;if(!k)k="";var ta=document.createElement("textarea");ta.appendChild(document.createTextNode(k));ta.cols=50;ta.rows=50;document.body.appendChild(ta)};xm lhttp.send(null);void(0)

CJX
04-10-2006, 12:42 PM
How could that be adapted to work with frames?

I mean, the script I am trying to write needs to get the source from another website, so hrhoo's method won't work. All I need is the base code of the page. The script needs to run from an HTML page, too.

I'm not hugely fluent in javascript, and I don't really understand the code you've provided.

Ultimater
04-10-2006, 12:52 PM
It would be best to use PHP. If you are running this locally on your PC, you can use Mrhoo's method if you rename your file extension to HTA (so it runs as an application under IE and avoid the cross-domain secuirty issue). Do you have a server? If so, I can write you up the PHP needed to make this work and the JavaScript to go with it to get what you needed done.

Ultimater
04-10-2006, 01:26 PM
A little something I just finished writting up:
http://www.aplustv.com/public_stuff/xmlhttprequester.htm

CJX
04-10-2006, 01:46 PM
Using PHP won't work.

I want to access a site's source code that can only be gotten while I'm logged in to that website. Therefore I thought that I could log in to that page, open it into a frame, and then use javascript to send my php files the source code of the pages.

Ultimater
04-10-2006, 02:04 PM
Ahh... Then in that case:

First Rename your top-most frame's file extension to HTA so JavaScript will work on foreign domains.
Second follow Mrhoo's approach.

<script type="text/javascript">
onload=function(){
var str= top.frames[0].document.documentElement.innerHTML;
var ta=top.frames[2].document.getElementsByTagName('textarea')[0];
ta.value=str;
}
</script>

If that doesn't work, try this:

<script type="text/javascript">
onload=function(){
var str= top.frames[0].document.documentElement.innerHTML;
var ta=top.frames[1].document.getElementsByTagName('textarea')[0];
ta.value=str;
}
</script>

or

<script type="text/javascript">
onload=function(){
var str= top.frames[1].document.documentElement.innerHTML;
var ta=top.frames[0].document.getElementsByTagName('textarea')[0];
ta.value=str;
}
</script>

You might have to play with the frame numers a bit til' you get it to work.

If the frames have names, you can use the name in quotes in place of the numbers.

It would be useful if you could elaborate more on the frame structure.

CJX
04-10-2006, 04:32 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>Web Processing</TITLE>
<script type="text/javascript">
var str= window.frames[1].document.documentElement.innerHTML;
var ta=window.frames[2].document.getElementsByTagName('textarea')[0];
ta.value=str;
</script>

</HEAD>
<FRAMESET cols="20%, 80%">
<FRAMESET rows="100, 200">
<FRAME src="stuff.php" name="js_f"><!-- Unrelated Stuff goes in here -->
<FRAME src="http://www.google.com" name="src_f"><!-- The Source Site -->
</FRAMESET>
<FRAME src="processor.php" name="out_f"><!-- The page with the textarea -->
<NOFRAMES>
Needs Frames.
</NOFRAMES>
</FRAMESET>
</HTML>

That is the code so far, for index.hta

When I run the script, I get an error:

Line: 7
Character: 2
Error: 'window.frames.js_f.document' is null or not an object
Code: 0

And it asks me if I want to continue running scripts.

Any more advice? I'll keep on experimenting.

Ultimater
04-10-2006, 05:56 PM
You are running the JavaScript prior to the frames existing. Run it onload:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>Web Processing</TITLE>
<script type="text/javascript">
onload=function(){
var str= window.frames[1].document.documentElement.innerHTML;
var ta=window.frames[2].document.getElementsByTagName('textarea')[0];
ta.value=str;
}
</script>

</HEAD>
<FRAMESET cols="20%, 80%">
<FRAMESET rows="100, 200">
<FRAME src="stuff.php" name="js_f"><!-- Unrelated Stuff goes in here -->
<FRAME src="http://www.google.com" name="src_f"><!-- The Source Site -->
</FRAMESET>
<FRAME src="processor.php" name="out_f"><!-- The page with the textarea -->
<NOFRAMES>
Needs Frames.
</NOFRAMES>
</FRAMESET>
</HTML>

CJX
04-11-2006, 12:03 PM
Thanks! That above script works.