Click here to Skip to main content
Click here to Skip to main content

HTML Preview

By , 26 Aug 2002
 
image, 103kb
Screen capture of the actual HTML Preview explained in this article

 

What is HTML Preview

HTML Preview is a tool answering a need I have got for a couple of years, actually since I started storing tons of HTML archives on my hard drives. My problem is likely to be yours : how to preview HTML pages without actually double-clicking on each item thus waiting for Internet Explorer to render it.

The amazing thing is that out there on the Internet, you have hundreds if not thousands of image thumbnailer sharewares, but you don't have a single tool to build automatically and with ease an aesthetic preview of all HTML pages in a given folder.

So let it be there.

Playing with HTML Preview

Before you intend to read the remainder of this article (if you like, or if you can), you may play with the provided HTML source code by doing the following. Once downloaded htmlpreview.html:
  • copy this file in the target folder
  • of course you need html pages to play with,
  • double-click on it
  • answer Yes when IE asks you if ActiveX scripting should be let run
  • just watch!

Building the HTML Preview from scratch

The story begins with the Windows Explorer web folders feature. You know, that space hungry thing anyone disables after a couple of days, which shows a zoomed out view of a selected picture, html or even doc file. Well, as anyone certainly guesses, this feature is full of HTML behind the wheel. Read this MS article for more info. Namely, when the web folders are activated, the operating system looks for either a <user profile>\folder.htt file or for the default <windowsdir>\web\folder.htt file. This .htt file is in fact simple HTML with ActiveX scripting inside (remember to enable ActiveX scripting in your Internet configuration panel).

What I've done is figure out the actual elementary ActiveX component that builds the preview of one picture or document, and then put some application logic to reflect the fact that I want not a single preview in the output, but the preview of all HTML pages.

Below is the key data for this ActiveX :

  • ActiveX friendly name: ThumbCl class

  • CLSID : 1D2B4F40-1F10-11D1-9E88-00C04FDCAB92

  • Automation-enabled

  • Exposed methods:

    HRESULT displayFile([IN] BSTR filepath)
    HRESULT haveThumbnail([OUT] BOOL *bThumbnail)
  • Exposed properties :

    [propget] HRESULT freeSpace(/* [retval][out] */ long *);
    [propget] HRESULT usedSpace(/* [retval][out] */ long *);
    [propget] HRESULT totalSpace(/* [retval][out] */ long *);
Of course as anyone guesses the one and only method that is worth mentioning is that displayFile([IN] BSTR filepath). It uses the client area declared in the HTML <object...> tag to render a preview of the passed file.
In fact, in HTML, that's simple code like follows :
  <table>
    <tr>
      <td>
        <object id=Thumbnail_0 
                classid="clsid:1D2B4F40-1F10-11D1-9E88-00C04FDCAB92"
                width=120 height=100>
        </object>
      </td>
      <td>&nbsp;</td>
     </tr>
  </table>
  <script>
    function Init_0()
    {
       Thumbnail_0.displayFile( 'D:\\new_stuff\\codeproject_homepage.htm' )
    }
  </script>
So far that's easy. Now let's put this in true dirty Javascript code. In order to do this, we assume the following (to keep things simpler for the moment) :
  • we have an file enumerator called fc to browse all files of the current folder. fc.item() returns the current file, and fc.moveNext() switches to next.
  • we are working with a variable curdir, which holds the current folder being previewed.
  • thisfilename = htmlpreview.html
The Javascript code to do the previewing of all *.htm(l) pages is as follows :
<script language="javascript">
// purpose : use directory file enumerator to list all *.htm* files
//           and prepare a table cell to preview it using the preview ActiveX

// put the following line in comment if you don't want scaling
var imgsize = " width=160 height=140";
var clsid = " classid='clsid:1D2B4F40-1F10-11D1-9E88-00C04FDCAB92'";


var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc;

f = fso.GetFolder(curdir);
fc = new Enumerator(f.files); 
s = "";
sf = "<script language="'javascript'"> function Init() { ";
i = 0;

for (; !fc.atEnd(); fc.moveNext())
{
  // create an ActiveX instance for the preview of this html file
  name = "Thumbnail"+i
  s = "<object id=" + name + clsid + imgsize + "></"+"object>";
  if (fc.item().Name.indexOf(".htm",0)>-1 && fc.item().Name!=thisfilename)
  {
    // and add an hyperlink to play with
    s = "<a href='"+fc.item().Name+"'>" + s + "</a>";
    document.write(s);
    // attach initialisation code to it
    // .replace(/\\/g, "\\\\") replaces simple backslashes with 
    // double-backslashes
    s = name + 
       ".displayFile( '" + fc.item().Path.replace(/\\/g, "\\\\") + "');";
    sf += s;
    i++;
  }
}
sf += "} </"+ "script>";
document.writeln(sf);
</script>
Again sorry for those who don't speak Javascript.

OK, now before providing the full uncensored listing, let's discuss the initial steps : When you double-click on htmlpreview.html in any folder, we begin with retrieving the full qualified path of htmlpreview.html, for instance c:\ Documents and Settings \ Administrator \ My Documents \ htmlpreview.html. But instead of getting just that with :

  var fullpath = location.href;
we rather get something of the form file:/// c:/ Documents%20and%20Settings / Administrator / My%20Documents / htmlpreview.html because javascript is cross-platform by design, thus returns a file://-protocol compliant URI.
We remove, strip down, replace some stuff out of this one to prepare URIs that are meaning something for the web browser when htmlpreview.html is being rendered. That's why you'll see the following code :
<script language="javascript">

  var fullpath, curdir, thisfilename;



  fullpath = location.href; // fully qualified filepath 
  // of the form "file:///c:/Documents%20and%20Settings/Administrator/
  // My%20Documents/htmlpreview.html"

  // regexp weirdo
  fullpath = fullpath.replace(/file:\/\/\//,""); // remove file:///
  fullpath = fullpath.replace(/\//g,"\\"); // replace slash with backslash 
                                           // (for use on Windows OS)
  fullpath = fullpath.replace(/%20/g," "); // replace escaped space

  // split path and filename
  var iLastBackslash = fullpath.lastIndexOf("\\");

  // = htmlpreview.html
  var thisfilename = fullpath.substring( iLastBackslash+1, fullpath.length); 
  // remove filename from full path, we are just interested in the path
  var curdir = fullpath.substring( 0, iLastBackslash );
</script>
Ok now for full listing of htmlpreview.html :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HTML Preview </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" onload="Init()">
<script language="javascript">

  var fullpath, curdir, thisfilename;



  fullpath = location.href; // fully qualified filepath 
  // of the form "file:///c:/Documents%20and%20Settings/Administrator/
  //  My%20Documents/htmlpreview.html"

  // regexp weirdo
  fullpath = fullpath.replace(/file:\/\/\//,""); // remove file:///
  fullpath = fullpath.replace(/\//g,"\\"); // replace slash with backslash 
                                           //(for use on Windows OS)
  fullpath = fullpath.replace(/%20/g," "); // replace escaped space

  // split path and filename
  var iLastBackslash = fullpath.lastIndexOf("\\");

  // = htmlpreview.html
  var thisfilename = fullpath.substring( iLastBackslash+1, fullpath.length); 
  // remove filename from full path, we are just interested in the path
  var curdir = fullpath.substring( 0, iLastBackslash );

  // purpose : use directory file enumerator to list all *.htm* files
  //           and prepare a table cell to preview it using the preview ActiveX

  // put the following line in comment if you don't want scaling
  var imgsize = " width=160 height=140";
  var clsid = " classid='clsid:1D2B4F40-1F10-11D1-9E88-00C04FDCAB92'";


  var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc;

  f = fso.GetFolder(curdir);
  fc = new Enumerator(f.files); 
  s = "";
  sf = "<script language="'javascript'"> function Init() { ";
  i = 0;

  for (; !fc.atEnd(); fc.moveNext())
  {
    // create an ActiveX instance for the preview of this html file
    name = "Thumbnail"+i
    s = "<object id=" + name + clsid + imgsize + "></"+"object>";
    if (fc.item().Name.indexOf(".htm",0)>-1 && fc.item().Name!=thisfilename)
    {
      // and add an hyperlink to play with
      s = "<a href='"+fc.item().Name+"'>" + s + "</a>";
      document.write(s);
      // attach initialisation code to it
      // .replace(/\\/g, "\\\\") replaces simple backslashes with double-backslashes
      s = name + ".displayFile( '" + fc.item().Path.replace(/\\/g, "\\\\") + "');";
      sf += s;
      i++;
    }
  }
  sf += "} </"+ "script>";
  document.writeln(sf);

</script>
The hyperlinks we have inserted while generating the HTML code are of course enabled : just right-click on previews.

Last remark, if you would like to change the size of thumbnails, just change the imgsize value.

The sample Code project preview then becomes (fraction-only to avoid full size) :

image, 141kb

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Stephane Rodriguez.
France France
Member
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).
 
Need a fast Excel generation component? Try xlsgen.
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot WorkingmemberGaurav.Tekkie11 May '10 - 2:46 
I tried to run this Html Preview where i kept my 10 other files also but it displays only blank browser.Wud u lyk to hlp me?I'll be highly obliged.
gAURAV gERA

GeneralProblem in IMG tagmemberAnudeep Jaiswal - NOIDA24 Nov '08 - 22:52 
i have a img tag inside repeter control.. the source of img tag is another aspx page(ImagePreview.aspx)
 
images are stored in database in byte format. first i convert that byte array on ImagePreview.aspx....
 
the repeter control is in update panel....
 

the problem is the image is show only first time.. when i change index of dropdown to view another profile. the content of repeter change but image is still same.....
Questionvar fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc;memberGnarlhead99 Nov '07 - 15:30 
wtf, in IE7.0.5730.11 on XPSP2 I get the 'object expected' error at this line..
 
var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc;
 

 
In a page cut-pasted from a tutorial page from your arstdesign site (via Wayback machine), this error does not occur; the thumb controls show, but without content, not even images. The control does load (ActiveX prompting indicates that)
 
Odd thing is that I compared that code with the htmlpreview code here line by line and except for comments, they are identical, including of course the var=fso.. line mentioned. Bizarre.
 
Apart from the above, to date I've never been able to get any code involving
classid 1D2B4F40-1F10-11D1-9E88-00C04FDCAB92 to display anything except
images, and that only on a form in VB using the control.
 
Is a security setting the problem? Or when Webvw.dll had an html exploit patched in 2005, they broke or neglected something possibly. 99.9999999% of programmers don't care about this issue, thus I need to stop monkeying around with rush job half baked piece of Microsoft $#!+.
 


AnswerRe: var fso = new ActiveXObject("Scripting.FileSystemObject"), f, fc;memberGnarlhead99 Nov '07 - 16:46 

From a 2003 usenet post by some guy name Dan.
 
begin quote
"Found it -- and it's not good. This is from the MS Windows Knowledge base:
 
SYMPTOMS
If you install Windows XP Service Pack 1 (SP1), you may not be able to view
some Web content files in Thumbnails view in the My Computer window or in
 
Windows Explorer.
CAUSE
For stability and performance reasons, Windows XP SP1 makes Thumbnails view
unavailable for the following file types:
a.. HTML documents (.htm and .html files)
b.. XML documents (.xml files)
c.. Internet e-mail messages (.eml files)
d.. Internet news messages (.nws files)
STATUS
This behavior is by design.
 

This behavior is ticking me off."
end quote
 
It's all coming back to me now, the days before XP SP1. Not.
 
On faster machines of the future, with the Explorer shell running inside
a protected mode as with IE7 on Vista, they could reconsider.
GeneralHtmlPreview not workingmemberMohit Infosys28 Feb '06 - 22:24 
I tried to run this HTML and it is in the folder where 2 other HTML are there. Whenever the HtmlPreview.html is loaded in the browser blank page appears. Is there any browser level setting needs to done. Please help me I need this ASAP.
 
Thanks
Mohit
QuestionRe: HtmlPreview not workingmemberJobberBoy7 Jun '07 - 7:20 
Did anyone ever find out why this was not working? I see posts where it does work for some but I also found that the page comes up and asks if I want to allow ActiveX and I answer yes. The page is blank with the cursor shown as a hand and the pages are shown in the status bar with their names as I move the cursor but no preview of html pages? Can anyone help me? I am using MS Windows XP Pro and Internet Explorer 6.
AnswerRe: HtmlPreview not workingmemberdarthbilly23 Jun '08 - 17:10 
In IE7, the error is on line 6:
 
<BODY BGCOLOR="#FFFFFF" onload="Init()">
 
So I presume that function has something funky going on as far as IE thinks?
GeneralSubfoldersmemberiamalive15 Feb '06 - 1:11 
Hi - this is just the tool I need!
 
Can you tell me how to make the search recursive and check all subfolders. Most of my html pages are stored in relevant folders to identify them. Checking each folder is what takes the time.
 
I'd be very grateful fo this tool if it can do that too.
 
Thanks a lot
GeneralRe: SubfoldersmemberStephane Rodriguez.15 Feb '06 - 23:22 

What about trying to understand the ten lines of code? Is it too much to ask?

GeneralRe: Subfoldersmemberiamalive15 Feb '06 - 23:52 
For a non-programmer, yes!
 
It may interest you to know that I have found a program that does a lot of this at http://rcmedia.town-local.net called Web Thumer. It doesn't generate the html pages, but it does capture the 2'000 + HTML pages I have recursively in all the folders.
 
Generating a neat page like yours, would be the next logical step. Perhaps a collaboration? I'm sending him your info.
 
David
GeneralRe: SubfoldersmemberStephane Rodriguez.16 Feb '06 - 6:47 

>> Perhaps a collaboration? I'm sending him your info.
 
Are you f***ing kidding me? Come on dude.
GeneralRe: Subfoldersmemberiamalive16 Feb '06 - 7:49 
I guess you must be French, Dude!
Generalreverse conceptmembersimonelli20 Jul '04 - 7:32 
I found this script and can see how some code of this would help me in the current project. maybe you could help me
 
I need for a text field in a form to strip down the full path and only save the filename.
 
How could i do this using JavaScript? i am guessing it would use the same concept as you used to replace the c:\ to file:, but i instead need it to replace all of it with nothing, so it only saves file.jpg and not c:\directory\other directory\file.jpg
 
thanks in advance
Jose
QuestionHow to do HTML Preview in Web Publishing form?membercopchen29 Jul '03 - 16:29 
Confused | :confused: I have downloaded the sample file "htmlpreview.html", then I double-click on it, it works!
 
But, when I put the file on my Web publishing path, using the form "http://...." to visit the page, it failed! I have nothing in the browser!
 
I have put the file on the folder "C:\thumbnail", but I don't know how to change the code "Thumbnail_0.displayFile( 'C:\\thumbnail\\myfile.htm' )"
 
Mr. Stephane Rodriguez, would you please help me!
Or somebody else, who can help me?
Thank you!
 

 
My code is as follows:
 
<BODY BGCOLOR="#FFFFFF" önload="Init_0()">
<object id=Thumbnail_0
classid="clsid:1D2B4F40-1F10-11D1-9E88-00C04FDCAB92"
width=120 height=100>
</object>
 
<script>
function Init_0()
{
Thumbnail_0.displayFile( 'C:\\thumbnail\\myfile.htm' )
}
</script>
</BODY>

Generalwhy no display in ie6+sp1memberueb28 Jul '03 - 16:06 
i update my windowsxp to sp1, then thumbnail dispeared. Confused | :confused:
GeneralRe: why no display in ie6+sp1memberAndrew Jackson1 Oct '03 - 1:07 
D'Oh! | :doh:
I get the same thing .. however I think this is related to IE6 SP1 and not the OS since this happens on W2K and XP.
Questionuseful?memberlexxwern1 Sep '02 - 3:55 
well its a neat article, but with previews of html files being available on windows os since win98, im not too sure of how much use it will be. but still useful for other os'.
 
<offtopic>
is that you in the pic?
</offtopic>
AnswerRe: useful?member.S.Rod.22 Dec '02 - 0:28 
lexxwern wrote:
well its a neat article, but with previews of html files being available on windows os since win98, im not too sure of how much use it will be. but still useful for other os'.
 
How do you do an html thumbnail within an app ? The fact that W2K+ supports it in Windows Explorer doesn't help you at this point.

Questioncan it be done with...sussnattu9726 Aug '02 - 20:11 
Methinks the same thing be achieved using IFrames.
We can specify a URL, and the size of the IFrames to simulate a thumbnail kind of look.
 
-srmSmile | :)
AnswerRe: can it be done with...memberStephaneRodriguez26 Aug '02 - 20:17 
Yes!Big Grin | :-D Just go on,
 

-- modified at 9:53 Tuesday 11th October, 2005
AnswerRe: can it be done with...sussAnonymous21 Jul '03 - 11:39 
I'd like to hear more about this two...I'd like to see something with more cross browser support.
 
Could the activex object be invoked server side somehow and write the image to a disk location?

GeneralRe: can it be done with...memberStephane Rodriguez.21 Jul '03 - 18:26 
You need a different plumbing for that purpose.
For instance, a server-side page could instantiate that ThumbnailCtrl ActiveX using C++, then ask the ActiveX to draw itself thanks to the IViewObject interface. For this you need to write C++ in another ActiveX, and the ASP page (or PHP script) would instantitate the ActiveX object on server-side.
 
VisualStudio provides ATL wizards to build the ActiveX from scratch.
 
Good luck!

 
-- modified at 9:53 Tuesday 11th October, 2005
GeneralPlease watch words more carefullymemberSamy Abd El-Rahman26 Aug '02 - 20:07 
How can you say to the reader:
If you dont have......you are stupid
I think it is not suitable...
 
Thanks

 
Samy Abdul-Rahman
Development Manager
Harf Informatiom Technology
GeneralRe: Please watch words more carefullyeditorNishant S26 Aug '02 - 20:36 
Hello Samy
 
I couldn;t find the quoted phrase in the article? Confused | :confused:
 
Nish
 

Author of the romantic comedy

Summer Love and Some more Cricket [New Win]

Review by Shog9
Click here for review[NW]

GeneralRe: Please watch words more carefullymemberSamy Abd El-Rahman26 Aug '02 - 20:43 
It seems that the article have been just updated Wink | ;)
 
Samy Abdul-Rahman
Development Manager
Harf Informatiom Technology

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 27 Aug 2002
Article Copyright 2002 by Stephane Rodriguez.
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid