 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 26th, 2003, 07:32 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How do you save an html page on your local hd?
Hi, I was wondering if anyone here knows this:
You open an html page opened by javascript, and you want to save it to your local hard drive, but not with the menu File->Save as, but rather, automatically, that is, page opened, page saved on C:
Can anyone please help? It's very important.
If you can include a piece of source code for this, it would really be helpful. Thanks!
|
|

August 26th, 2003, 08:35 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
OTMH the only way I can think to do this is Windows/IE specific. You can use document.documentElement.outerHTML to get the HTML source for the page, then use the FileSystemObject to save it off to disk. You'll need Low security settings to get this to work without a prompt. Example below:
Code:
<HTML>
<HEAD>
<script LANGUAGE="JavaScript">
function SaveToDisk(sPath)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileDest = fso.CreateTextFile(sPath, true);
if (fileDest)
{
fileDest.Write(document.documentElement.outerHTML);
fileDest.close();
}
else
{
alert("unable to create file " + sPath);
}
}
</script>
</HEAD>
<BODY onload="SaveToDisk('c:\\temp\\123.htm');">
<P>The rest of the page is here...</P>
</BODY>
</HTML>
hth
Phil
|
|

August 27th, 2003, 11:08 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you so much, Phil! This was very helpful!~
|
|

August 27th, 2003, 02:51 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This script is for saving its own html code, however, could it be modified for it to save, say another page? For instance, if i have an URL like yahoo.com or whatever, could this script be modified as to save that URL somehow?
Thanks so much!,
Roberto
|
|

August 28th, 2003, 02:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Yes and No :)
You would have to find another method to get the HTML source of the page from the other URL becuase it would obviously not be in document.documentElement.outerHTML.
Maybe you could put the URL you want as the src of a hidden IFRAME and refer to that instead? Or maybe scrape it with Microsoft.XMLHTTP component?
rgds
Phil
|
|

August 28th, 2003, 07:13 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Phil, if you have a piece of code of the IFRAME usage example or how to scrap the web page with the Microsoft.XMLHTTP I'd be very helful, because I don't know much about JavaScript, and I belive these are not very standard things to do in JavaScript.
Thanks, 
Roberto
|
|

August 28th, 2003, 08:15 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Here you go Roberto, this piece of code uses the XMLHTTP object from MSXML v4.
Code:
<HTML>
<HEAD>
<script LANGUAGE="JavaScript">
function SaveToDisk(sUrl, sPath)
{
var x = new ActiveXObject("Msxml2.XMLHTTP.4.0");
x.open("GET", sUrl, false);
x.send();
if (x.status == 200)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileDest = fso.CreateTextFile(sPath, true, true);
if (fileDest)
{
fileDest.Write(x.responseText);
fileDest.close();
}
else
{
alert("unable to create file " + sPath);
}
}
else
{
alert("HTTP error status " + x.status + " from " + sUrl);
}
}
</script>
</HEAD>
<BODY onload="SaveToDisk('http://www.p2p.wrox.com', 'c:\\temp\\123.htm');">
<P>The rest of the page is here...</P>
</BODY>
</HTML>
|
|

August 28th, 2003, 10:37 AM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I get this error message: Line 6, char 9: "Error: Automation server can't create object", is there a possibility to do something about this? Like configuring my explorer to be able to create that object or something?
|
|

August 29th, 2003, 02:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
It depends where that line is. If it's where the "Msxml2.XMLHTTP.4.0" is created it's likely you don't have the classes installed, try changing to "Msxml2.XMLHTTP.3.0" and if this doesn't work install xml core services version 4 from Microsoft. If it's where the "FileSystemObject" is created you have your security settings too high for this. Most users won't allow you to save to their hard drive, if you are in control of settings etc., maybe this is for an intranet page and the status bar shows the page as in the trusted sites or local intranet, then change the settings in the security section of Tools | Internet options to enable ActiveX not marked safe for scripting.
--
Joe
|
|

August 29th, 2003, 01:12 PM
|
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks, Joe, worked just fine with the 3.0.
Best regards,
Roberto
|
|
 |