Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an HTML file from a batch file in default browser with a specific destination anchor?

Tags:

batch-file

I am trying to open an HTML file with a specific destination anchor from a batch file like so:

start iexplore %~dps0nl752.htm#01
exit    

nl753.htm is on the local drive.

How can I get Windows to open the HTML file with the destination anchor in the default browser instead of Internet Explorer?

like image 448
David Mills Avatar asked Dec 31 '16 19:12

David Mills


1 Answers

If you want to start the default browser then you should obviously not hardcode iexplore! The start command will figure out the system default on its own based on the file extension or URL protocol.

On my Windows 8.1 machine anchors work fine:

start "" "http://example.com#whatever"

it does seem to work without quotes as well but I personally prefer them just in case there is a space or a & there.

In my experience Internet Explorer will sometimes do some kind of internal redirection when dealing with local .htm[l] files and/or file:// URLs and this seems to strip away the anchor. There is not much you can do about that.

Just to be clear, appending a anchor to a plain file system path will not work because it technically changes the extension. A file:// URL will work because Windows only looks at the protocol when dealing with URLs.

On my system start "" "file:///C:/Program%20Files/Common%20Files/microsoft%20shared/Stationery/Soft%20Blue.htm#whatever" does work as far as accepting the command and starting the browser. This does not mean that the browser will respect the anchor but that is a implementation detail in the browser that you have no control over and results might vary depending on which browser is the default.

If this is not good enough then you can try using IE Automation to start and control Internet Explorer. This means you have to give up using the users default browser and I would not recommend it for this reason...

Relying on the file:// protocol and a .html extension to execute a browser and not a HTML editor is somewhat risky as pointed out in another answer but I'm not sure if I would dare to query the registry in a batch-file either.

The absolute best solution is to call ShellExecuteEx with a forced progid but that is only possible in a real application.

The second best solution is to write a Windows Scripting Host script instead of a batch file since it is much safer to read and parse the registry there...

like image 53
Anders Avatar answered Nov 08 '22 17:11

Anders