Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 26th, 2003, 07:32 AM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
  #2 (permalink)  
Old August 26th, 2003, 08:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #3 (permalink)  
Old August 27th, 2003, 11:08 AM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much, Phil! This was very helpful!~

Reply With Quote
  #4 (permalink)  
Old August 27th, 2003, 02:51 PM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old August 28th, 2003, 02:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #6 (permalink)  
Old August 28th, 2003, 07:13 AM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #7 (permalink)  
Old August 28th, 2003, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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>
Reply With Quote
  #8 (permalink)  
Old August 28th, 2003, 10:37 AM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

Reply With Quote
  #9 (permalink)  
Old August 29th, 2003, 02:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
Reply With Quote
  #10 (permalink)  
Old August 29th, 2003, 01:12 PM
Authorized User
 
Join Date: Aug 2003
Location: San Jose, N/A, Costa Rica.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Joe, worked just fine with the 3.0.

Best regards,
Roberto

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html form - save help bunnyhead HTML Code Clinic 2 January 24th, 2005 05:55 AM
can't save html /css website !! ca123 CSS Cascading Style Sheets 2 November 15th, 2004 07:29 PM
How to save string value as html file? android66 Javascript 0 September 3rd, 2004 12:00 AM
html page save to excel format mateenmohd Javascript 2 January 17th, 2004 05:48 AM
how to save html output of xslt to file wtalbin XSLT 2 August 15th, 2003 09:14 PM



All times are GMT -4. The time now is 06:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.