Sign in Join
FPDoc Onlineexperiment

OpenURL function

In unit LCLIntf

Opens a given URL with the default browser for the system.

Declaration

pascal
function OpenURL(AURL: string): Boolean

Arguments

AURL

URL with the resource to open in the browser application.

Function result

True a browser application was found and called to open the specified URL.

Description

OpenURL is a routine used to open the specified URL using the default browser application for a platform or host operating system. The implementation is platform-specific.

AURL is passed as parameter to the browser application. AURL must be a URL like https://www.lazarus-ide.org/ or file:///tmp/test.txt or file://C:\test.txt. AURL is quoted when passed as an argument to ensure that space(s) in the path or file name are handled properly.

For UNIX-like environments (including Linux), FindDefaultBrowser is used to find the default browser application for the platform. It searches the PATH environment for one of the pre-defined browser applications (or helpers) such as:

  • 'firefox'
  • 'mozilla'
  • 'google-chrome'
  • 'galeon'
  • 'konqueror'
  • 'safari'
  • 'netscape'
  • 'opera'
  • 'iexplore'
  • 'htmlview'
  • 'xdg-open'

If a browser application was found, the complete path to the executable is used to execute the application using AURL as an argument. AURL must be a valid URL like http://www.lazarus.freepascal.org or file:///tmp/test.txt or file://C:\test.txt. Space characters in the AURL must be encoded using the %20 notation required for URLs as described in RFC 1738 (http://www.ietf.org/rfc/rfc1738.txt).

RunCmdFromPath is used to launch the process and wait for its completion. The return value is True on exit.

For the Windows environment, the actions are a little less straight-forward (of course they are). Windows tries to locate rundll32 and url.dll to build the command line with parameters needed for the default browser application. From this it determines the Windows App ID. ShellExecute is called to run the browser application and get its process handle. Common browser applications like Chrome, Firefox, Edge, and Internet Explorer are checked. Edge seems to work the best.

For the macOS platform, the 'open' command is used to launch a url in the appropriate browser. Foundation classes are used to get the URL and the shared workspace where the URL is opened. The process fails if AURL does not contain a valid protocol specifier like 'https://' or 'file://'.

If a browser application is not found, or the suggested file is not an executable, no additional actions are performed in the routine and the return value is False.

For the Android platform, the routine assigned to the OpenURLWidgetsetImplementation variable is used instead of the default browser lookup and RunCmdFromPath.

OpenURL is not implemented for the Amiga platform.

See also

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.