Solved

Viewing a document in a web browser with and without window reuse

Posted on 2001-09-03
3
147 Views
Last Modified: 2010-04-06
Hi,

I need to be able to view an HTML document in a web browser.  I really need something like this...

procedure ViewDocument(BrowserPath, DocumentURL: string; ReuseBrowserWindow: Boolean);

BrowserPath is the full path of any browser (such as IE or Netscape), DocumentURL is the URL of the HTML file to view, this can be either on the web or a local file.

The last parameter is the most important.  If ReuseBrowserWindow if False, the document should always open up in a new window.  However if it is True, it should reuse a browser window that is already opened and only open a new one if one isn't presently opened.

Is the above possible?  If so, would you be able to give me any source code that would do it in Delphi 4?

Also, there is the possibility that the procedure may fail for any reason when being called.  In order to make the program robust, what is the best way of dealing with this?  Should it be made a function that simply returns True/False for success/failure or should it raise an exception if this happens?  I'd be interested to know what is the best programming practice to use and what is most widely used.  I often wonder when programming, should I make a function return True/False to indicate success/failure or use exceptions when something goes wrong and expect the caller to handle this case?

Thanks a lot for your help.

Richard
0
Comment
Question by:Richard2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
alanwhincup earned 50 total points
ID: 6451515
Ive got half of it:

In the below example ive missed out the BrowserPath part in the procedure and the URL just opens up in the default browser. I'll try and improve it to do the full thing for you:

procedure ViewDocument(DocumentURL: string; ReuseBrowserWindow: Boolean);
var
  BrowserPath : string;
begin
  if ReuseBrowserWindow then
    Shellexecute(0, 'Open', PChar(DocumentURL), nil, nil, SW_SHOWNORMAL)
  else
  begin
    with TRegistry.Create do
    begin
      try
        RootKey := HKEY_CLASSES_ROOT;
        OpenKey('\htmlfile\shell\open\command', False);
        try
          BrowserPath := ReadString('');
        except
          MessageDlg('Default Browser could not be found.', mtError, [mbOK], 0);
          Exit;
        end;
        CloseKey;
      finally
        Free;
      end;
    end;
    BrowserPath := Copy(BrowserPath, Pos('"', BrowserPath) + 1, Length(BrowserPath));
    BrowserPath := Copy(BrowserPath, 1, Pos('"', BrowserPath) - 1);
    ShellExecute(0, 'Open', PChar(BrowserPath), PChar(DocumentURL), nil, SW_SHOWNORMAL);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ViewDocument('www.experts-exchange.com', False);
end;

Cheers,

Alan
0
 
LVL 3

Expert Comment

by:rondi
ID: 6452597
For starters, ShellExecute would be a lot more concise - it'll also return error info if you need it.

IE has a setting in the Internet Options dialog: "Browse in a new process". The same setting applies to the Windows Explorer.

So it looks like you won't be able to control the window
behaviour by simply launching the browser - unless you
read and change the property from the registry (check out HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\ and see value SeparateProcess).

There's probably a way to do it via COM. Check out the
type library.

rondi.
0
 

Author Comment

by:Richard2000
ID: 6469007
Hi,

Thanks for your code and comments.  I've tried the code out using IE 5 as my default browser and it seems to work well.

However, is it possible to make it work by giving it the path of a browser as a parameter (rather than using the default one)?  This should be any browser e.g. Netscape or Opera as well as IE.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month4 days, 4 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question