Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting source code into a variable 1

Status
Not open for further replies.

jonthequik

Programmer
Aug 6, 2000
40
0
0
US
I am trying to figure out if there is a way in JavaScript to get the source code out of a page, and into a variable. I know that you can get the code with a button value of "view-source: but I need it to load into a variable instead of an editor.

Any ideas? Jonathan Hannan
Computer Repair, Webdesign
HTML, CGI, PERL, JavaScript, XML
 
you mean, you want to upload files in a variable ? coz the source of a page is the file that created this page, right ?
 
if you give your preliminary <HTML> tag an id:

<html id=&quot;wholedoc&quot;>

you can get:

<body onLoad=&quot;allcode=wholedoc.innerHTML&quot;>

I tested this in IE5.5, but it should work starting with 4 I think and also NS6. jared@aauser.com
 
but that won't give you the full source, Tom. Try the code above, it will give you scripts and the head section - everything. All you need to remember is that to make the full document, you must add a beggining and ending <HTML> tag. jared@aauser.com
 
to add the id to the html tag, the file has to be yours ... i don't really see the point in trying to find a way to edit your own code (i mean, not in order to code) ... maybe there is but i don't see ...
 
You can't read the document.body of a page that's not yours - for instance in an IFRAME if you load msn.net getting the innerHTML of the body will give you an access is denied error jared@aauser.com
 
yeah, it doesn't work, it has to have an id jared@aauser.com
 
The innerHTML and innerText require an id to work. I've found a way to capture source code with a CGI module, but I've encounted another problem.

The game I am playing uses a cookie to verify login. If I change browsers (close or use another window, it won't let me continue without logging back in.)

The game I play has a status report on displayed from a Report.cgi file. When I try to get access through my own CGI Module, the game recognizes that I'm access from outside the browser and asks me to login. Now I'm back to square one. I've searched the internet and found ways to get the source code to display in notepad through javascript, but there is no known way of getting the information with JavaScript. Jonathan Hannan
Computer Repair, Webdesign
HTML, CGI, PERL, JavaScript, XML
 
Well, do you require script code in the head?

You can use self.document.body.innerHTML to get the body source, and self.document.title to get the title if you want it. That's the best you can do if you can't give the HTML or HEAD an id.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
You can also access the scripts with document.scripts[index].innerHTML jared@aauser.com
 
Jared, I think that only gives you the scripts that were defined in the body. Do you know otherwise?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Cool, then that is how you do it:

document.body.innerHTML
document.title
document.scripts

That'll give you the full source, excluding meta tags and perhaps some comments.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
but, if you want the entire source of a document.... i like to use perl, an example:


#!/usr/bin/perl
use LWP;
use HTTP::Cookies;
use HTTP::Request;
$| = 1;

$ua = new LWP::UserAgent;
$ua->cookie_jar(HTTP::Cookies->new(file => &quot;lwpcookies.txt&quot;, autosave => 1));
$ua->agent('Mozilla/4.0');
$request = HTTP::Request->new(GET => 'print &quot;Retrieving Data...\n&quot;;
$response = $ua->request($request);
$res = $response->content;
print $res;
 
styles:

document.all.tags(&quot;STYLE&quot;)[index].innerHTML
jared@aauser.com
 
the perl code will print to the screen the entire contents of the page, pretend that its a browser and accept cookies.


pretty neet, isnt it?
 
luciddream, I believe he said that it had to be done client-side while he's playing the game, otherwise, yes, Perl would be my first suggestion.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top