5 Send URL: Opening a Web Browser

▼ |  | Net: Networking Libraries

1 | HTTP Client
2 | URLs and HTTP
3 | URI Codec: Encoding and Decoding URIs
4 | FTP: Client
5 | Send URL: Opening a Web Browser
6 | SMTP: Sending E-Mail
7 | sendmail: Sending E-Mail
8 | Headers: Parsing and Constructing
9 | Header Field Encoding
10 | IMAP: Reading Mail
11 | POP3: Reading Mail
12 | MIME: Decoding Internet Data
13 | Base 64: Encoding and Decoding
14 | Quoted-Printable: Encoding and Decoding
15 | DNS: Domain Name Service Queries
16 | NNTP: Newsgroup Protocol
17 | TCP: Unit and Signature
18 | TCP Redirect: tcp^ via Channels
19 | SSL Unit: tcp^ via SSL
20 | CGI Scripts
21 | Cookie: Legacy HTTP Client Storage
22 | Git Repository Checkout
| Bibliography
| Index

On this page:

send-url
send-url/ file
send-url/ contents
send-url/ mac
external-browser
browser-preference?
browser-list
unix-browser-list

top contents ← prev up next →

### 5`` Send URL: Opening a Web Browser🔗ℹ

(require net/sendurl) | package: net-lib

Provides send-url for opening a URL in the user’s chosen web browser.

See also browser/external, which requires racket/gui, but can prompt the user for a browser if no browser preference is set.

> > procedure
> > (send-url |  | str |  |  |  |
> > | [ | separate-window? |  |  |  |
> > |  | #:escape? escape?]) |  | → |  | void?
> str : string?
> separate-window? : any/c = #t
> escape? : any/c = #t

Opens str, which represents a URL, in a platform-specific manner. In particular, the first value in browser-list will determine which browser will be used to open the URL. For some platforms and configurations, the separate-window? parameter determines if the browser creates a new window to display the URL or not.

If escape? is true, then str is escaped (by UTF-8 encoding followed by “%” encoding) to avoid dangerous shell characters: single quotes, double quotes, backquotes, dollar signs, backslashes, non-ASCII characters, and non-graphic characters. Note that escaping does not affect already-encoded characters in str.

There are two ways to override the above behavior: the external-browser parameter and the 'external-browser preference. A valid setting for both the external-browser parameter and the 'external-browser preference must satisfy browser-preference?, with a restriction that the setting for the 'external-browser preference cannot be a procedure. The external-browser parameter takes priority over 'external-browser preference: the preference is only used when (external-browser) is #f. See put-preferences for details on setting preferences.

On Unix, it’s recommended to not override the default behavior, but to rely on xdg-open in browser-list.

> > procedure
> > (send-url/file |  | path |  |  |  |
> > | [ | separate-window? |  |  |  |
> > |  | #:fragment fragment |  |  |  |
> > |  | #:query query]) |  | → |  | void?
> path : path-string?
> separate-window? : any/c = #t
> fragment : (or/c string? #f) = #f
> query : (or/c string? #f) = #f

Similar to send-url (with #:escape? #t), but accepts a path to a file to be displayed by the browser, along with optional fragment (with no leading #) and query (with no leading ?) strings. Use send-url/file to display a local file, since it takes care of the peculiarities of constructing the correct file:// URL.

The path, fragment, and query arguments are all encoded in the same way as a path provided to send-url, which means that already-encoded characters are used as-is.

> > procedure
> > (send-url/contents |  | contents |  |  |  |
> > | [ | separate-window? |  |  |  |
> > |  | #:fragment fragment |  |  |  |
> > |  | #:query query |  |  |  |
> > |  | #:delete-at seconds]) |  | → |  | void?
> contents : string?
> separate-window? : any/c = #t
> fragment : (or/c string? #f) = #f
> query : (or/c string? #f) = #f
> seconds : (or/c number? #f) = #f

Similar to send-url/file, but it consumes the contents of a page to show and displays it from a temporary file.

When send-url/contents is called, it scans old generated files (this happens randomly, not on every call) and removes them to avoid cluttering the temporary directory. If the seconds argument is a number, then the temporary file is more eagerly removed after the specified number of seconds; the deletion happens in a thread, so if Racket exits earlier, the deletion will not happen. If the seconds argument is #f, no eager deletion happens, but old temporary files are still deleted as described above.

> procedure (send-url/mac url [#:browser browser]) → void?
> url : string?
> browser : (or/c string? #f) = #f

Like send-url, but only for use on a Mac OS machine.

The optional browser argument, if present, should be the name of a browser installed on the system. For example,

> (send-url/mac "https://www.google.com/" #:browser "Firefox")

would open the url in Firefox, even if that’s not the default browser. Passing #f means to use the default browser.

> parameter (external-browser) → browser-preference?
> (external-browser cmd) → void?
> cmd : browser-preference?

A parameter that can hold a browser preference to override how a browser is started for send-url. See browser-preference? for details.

> procedure (browser-preference? a) → boolean?
> a : any/c

Returns #t if v is a valid browser preference, #f otherwise. A valid browser preference is either:

- The value #f, which falls back to the next preference method.

- A procedure? that accepts a URL string. This value is not allowed for the 'external-browser preference.

- A symbol in browser-list that indicates a browser to use.

- A pair of strings to be concatenated with a URL string to form a shell command to run. The first string is the command prefix and the second string is the command suffix. This method requires extra care:

  - The URL can hold characters like #, ?, and &, so the pair of strings should place double quotes around the URL.

  - The URL should be encoded to make it work inside shell double-quotes, so the default value of escape? in send-url should be used.

> value browser-list : (listof symbol?)

A list of symbols representing executable names that may be tried in order by send-url. The send-url function internally includes information on how to launch each executable with a URL.

Added in version 7.5.0.10 of package net-lib.

> value unix-browser-list : (listof symbol?)

> NOTE: This value is deprecated; use browser-list, instead.

The same as browser-list.

Changed in version 7.5.0.10 of package net-lib: Changed the value to be an alias of browser-list.

top contents ← prev up next →