Mht JavaScript Reference Documentation
Mht
Current Version: 11.5.0
Chilkat.Mht
Convert a web page, local HTML file, or HTML string into a self-contained
MHT archive with related resources embedded.
Produce email-friendly MIME output from HTML content for workflows that
need to send or store HTML pages as messages.
Download and include images, CSS files, frames, and other supporting
resources referenced by the HTML.
Use proxy settings, authentication, caching, and related options when
fetching web pages and their dependent resources.
Save generated MHT or EML content to files, return it in memory, or place
the resulting output into a ZIP archive.
Extract an existing MHT archive into an HTML file plus supporting
resource files for inspection, editing, or republishing.
For an extended overview, see
Mht Class Overview.
Create, save, zip, email, and unpack MHT web archives.
Chilkat.Mht creates MHT web archives and EML email messages
from web pages, local HTML files, or in-memory HTML strings. It can download
and embed related resources such as images, style sheets, frames, and other
linked content, optionally include scripts for MHT output, use disk caching,
support proxies and authentication, save or zip the generated output, and
unpack existing MHT files back into HTML and supporting files.
Create MHT archives
Generate EML messages
Embed page resources
Network configuration
Save or zip output
Unpack MHT files
Mht when an application needs to capture a web page or HTML
document together with its dependent resources as a single archive or email
message. Provide the URL, local HTML file, or HTML string, configure any
proxy, caching, authentication, or resource-handling options, then save the
result as MHT, EML, memory data, or zipped output.
Object Creation
Note: This is intended for running within a Chilkat.Js embedded JavaScript engine.
var obj = new CkMht();
Properties
AbortCurrent
· boolean
Set to true to request cancellation of the operation currently running on this object. Long-running network and file operations periodically check this property; a fast operation may complete before the request is observed.
A synchronous method can be cancelled from another thread by setting this property on the same object. Chilkat resets the property to false after an abort is processed, and a stale request is cleared when a later method begins.
BaseUrl
· string
Specifies the base URL used when the source is a local HTML file or an in-memory HTML string. Relative links and resource references are resolved against this value before MHT or EML output is created.
BaseUrl when HTML supplied from a file or string contains relative references such as images/logo.png or ../styles/site.css.ConnectTimeout
· int
Specifies the maximum number of seconds allowed for establishing a connection to an HTTP server. The default is 10 seconds.
ReadTimeout to control inactivity while receiving data.DebugHtmlAfter
· string
Specifies a local file path where Chilkat saves the HTML after it has been processed for MHT or EML generation. Leave the property empty to disable this diagnostic output.
DebugHtmlBefore to inspect rewritten URLs, removed scripts, and other transformations applied before the MIME document is assembled. Do not enable diagnostic output to a sensitive or publicly accessible location.DebugHtmlBefore
· string
Specifies a local file path where Chilkat saves the source HTML before it is transformed for MHT or EML generation. Leave the property empty to disable this diagnostic output.
DebugHtmlAfter to determine whether a problem originates in the input HTML or in the conversion and URL-rewriting process.DebugLogFilePath
· string
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
EmbedImages
· boolean
Controls whether images referenced by the HTML are downloaded or read from disk and embedded as related MIME parts. When false, image references remain external; relative image URLs are converted to absolute URLs when sufficient base information is available.
EmbedLocalOnly
· boolean
When true, only images referenced from the local filesystem are embedded. Images referenced by HTTP or HTTPS URLs remain external. This setting is meaningful when EmbedImages is true.
FetchFromCache
· boolean
When true, Chilkat attempts to satisfy page and resource requests from the configured disk cache before downloading them. Add one or more cache directories with AddCacheRoot. The default is false.
UpdateCache controls whether network responses are written to the cache. IgnoreMustRevalidate and IgnoreNoCache control whether certain HTTP cache directives are honored.IgnoreMustRevalidate
· boolean
Controls how cached responses containing Cache-Control: must-revalidate are handled. When false (the default), Chilkat revalidates such a cached response before reuse. When true, a still-fresh cached response may be used without revalidation. This property is relevant only when FetchFromCache is true.
must-revalidate can improve offline or high-latency operation, but it may also use content the origin server intended to be checked before reuse.IgnoreNoCache
· boolean
Controls whether HTTP response directives that prohibit or restrict caching are ignored. The default is false, which honors the server's caching instructions. When true, Chilkat may store responses that would otherwise be excluded from the disk cache.
LastErrorHtml
·string,read-only
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
·string,read-only
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
·string,read-only
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
· boolean
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
NoScripts
· boolean
Controls script removal when creating MHT output. When true, script content is removed. When false (the default), scripts may be preserved in MHT output according to the other script-related settings.
NtlmAuth
· boolean
When true, Chilkat uses NTLM authentication when the origin HTTP server requests authentication. The default is false. Set WebSiteLogin, WebSitePassword, and, when required, WebSiteLoginDomain.
ProxyLogin and ProxyPassword. Use HTTPS whenever credentials or authenticated content are transmitted.NumCacheLevels
· int
Specifies the number of directory levels used beneath each disk-cache root. The default is 0, which stores cached items directly in the root. A value of 1 distributes items among 256 subdirectories named 0 through 255. A value of 2 uses two such levels, for example 17/204. Chilkat creates the required subdirectories automatically.
NumCacheRoots
·int,read-only
Returns the number of disk-cache root directories currently configured through AddCacheRoot. Multiple roots allow cached items to be distributed across different directories or storage volumes.
GetCacheRoot to retrieve a configured root by index.PreferIpv6
· boolean
Controls address-family preference when a hostname resolves to both IPv4 and IPv6 addresses. The default is false, which prefers IPv4. Set to true to prefer IPv6.
PreferMHTScripts
· boolean
Controls which content is retained when HTML contains a script together with a <noscript> alternative. When true (the default), the script is preferred and the <noscript> content is discarded. When false, the script is removed and the <noscript> content is retained.
NoScripts takes precedence when script removal is required. EML generation always removes scripts.Proxy
· string
Specifies an HTTP proxy as hostname:port, for example proxy.example.com:8080. Leave the property empty when no explicit HTTP proxy is required.
http:// or https:// scheme. If the proxy requires credentials, set ProxyLogin and ProxyPassword.ProxyLogin
· string
Specifies the username used to authenticate with the HTTP proxy configured by Proxy. Leave empty when the proxy does not require authentication.
ProxyPassword
· string
Specifies the password used to authenticate with the HTTP proxy configured by Proxy. Leave empty when the proxy does not require authentication.
ReadTimeout
· int
Specifies the maximum number of seconds that an HTTP read may remain inactive while waiting for additional data. The default is 20 seconds. If data continues to arrive, a long download may run longer than this value.
RequireSslCertVerify
· boolean
Controls verification of the TLS server certificate for HTTPS connections. When true, the connection is rejected if certificate verification fails, such as because the certificate is expired or its chain or signature cannot be validated. The default is false.
SocksHostname
· string
Specifies the hostname or dotted-decimal IPv4 address of the SOCKS proxy. This property is used only when SocksVersion is 4 or 5.
SocksPassword
· string
Specifies the password for SOCKS5 proxy authentication. SOCKS4 does not define password authentication, so this property is ignored when SocksVersion is 4.
SocksPort
· int
Specifies the TCP port of the SOCKS4 or SOCKS5 proxy. The default is 1080. This property is used only when SocksVersion is 4 or 5.
SocksUsername
· string
Specifies the username sent to the SOCKS4 or SOCKS5 proxy. It is used only when SocksVersion is 4 or 5. For SOCKS5, set SocksPassword when password authentication is required.
SocksVersion
· int
Selects whether requests are routed through a SOCKS proxy.
| Value | Behavior |
|---|---|
0 | Do not use a SOCKS proxy. This is the default. |
4 | Connect through a SOCKS4 proxy. |
5 | Connect through a SOCKS5 proxy. |
4 and 5, set SocksHostname and SocksPort. Configure username and password properties when the proxy requires them.UnpackDirect
· boolean
Controls whether an MHT archive is extracted without rewriting the HTML or relocating related resources into the normal parts subdirectory. The default is false.
| Setting | Behavior |
|---|---|
false | Related parts are written beneath the partsSubDir directory, and HTML references are rewritten to point to the extracted files. |
true | HTML is not rewritten. Related parts are written using their relative Content-Location paths beneath the unpack directory, and the partsSubDir argument is ignored. |
Content-Location values. Parts whose locations are full URLs cannot be mapped directly in this mode.UnpackUseRelPaths
· boolean
Controls how extracted resources are referenced from the rewritten HTML produced by the normal unpacking mode. The default is true, which uses relative paths. Set to false to write absolute local paths.
UpdateCache
· boolean
Controls whether successful HTTP GET responses are written to the configured disk cache. The default is false. When true, add at least one cache root with AddCacheRoot.
FetchFromCache controls cache reads. Applications may enable either setting independently or enable both for normal read-through caching.UseCids
· boolean
Controls how embedded resources are referenced in generated MHT and EML MIME documents. The default is true.
| Setting | Reference model |
|---|---|
true | Chilkat generates Content-ID values and rewrites HTML resource URLs as cid: references. |
false | HTML URLs remain unchanged, and each embedded MIME part receives a matching Content-Location header. |
cid: references are common in HTML email, while Content-Location preserves the original URL or path.UseFilename
· boolean
Controls whether Chilkat adds a filename parameter to the Content-Disposition header of each embedded resource, such as an image or style sheet. The default is true.
UseIEProxy
· boolean
When true, Chilkat uses the proxy configuration associated with Windows Internet Options / Internet Explorer. This setting is relevant on Windows systems.
Proxy when the application should specify a particular proxy directly rather than relying on the current user's Windows proxy settings.UseInline
· boolean
Controls whether embedded resources receive a Content-Disposition: inline header. The default is true. When false, Chilkat does not add the inline disposition for those resources.
VerboseLogging
· boolean
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
·string,read-only
WebSiteLogin
· string
Specifies the username used when the origin website requires HTTP authentication. It is optional for public pages. For NTLM authentication, also set NtlmAuth to true and provide a domain when required.
WebSiteLoginDomain
· string
Specifies the optional Windows domain used with NTLM website authentication. This property is relevant when NtlmAuth is true.
WebSitePassword
· string
Specifies the password used with WebSiteLogin when the origin website requires HTTP authentication.
Methods
AddCacheRoot
· return:undefined
· dir:string
Adds dir as a root directory for the disk cache. Call this method once for each cache location the application wants Chilkat to use. Multiple roots may be placed on different directories or storage volumes.
FetchFromCache and UpdateCache to enable cache reads and writes.AddCustomHeader
· return:undefined
· name:string
· value:string
Adds an HTTP request header to the requests sent while retrieving web pages and related resources. name is the header field name and value is its value. Call the method once for each custom header to add.
Authorization or X-Custom-Header without a trailing colon. Custom headers remain configured until removed with RemoveCustomHeader or cleared with ClearCustomHeaders.AddExternalStyleSheet
· return:undefined
· url:string
Adds the style sheet at url to the set of external CSS resources downloaded and embedded during conversion. This is normally unnecessary because Chilkat automatically discovers style sheets referenced directly by the HTML.
ClearCustomHeaders
· return: undefined
Removes all HTTP request headers previously added with AddCustomHeader. Built-in and automatically generated HTTP headers are not described as custom headers and are not the purpose of this method.
ExcludeImagesMatching
· return:undefined
· pattern:string
Adds pattern as an exclusion pattern for image URLs. Images whose URLs match an exclusion pattern are not embedded in the generated MHT or EML output.
GetAndSaveEML
· return:boolean;truefor success,falsefor failure
· url_or_htmlFilepath:string
· emlPath:string
Creates an EML email message from the web-page URL or local HTML file specified by url_or_htmlFilepath and saves it to emlPath. Referenced images and style sheets are fetched and embedded as related MIME parts.
emlPath is the destination EML file path; the method returns true on success and false on failure.Returns true for success, false for failure.
topGetAndSaveMHT
· return:boolean;truefor success,falsefor failure
· url_or_htmlFilepath:string
· mhtPath:string
Creates an MHT web archive from the web-page URL or local HTML file specified by url_or_htmlFilepath and saves it to mhtPath. Referenced resources are fetched and embedded according to the current MHT properties.
url_or_htmlFilepath is either an HTTP/HTTPS URL or a local HTML file path. mhtPath is the destination MHT file path. Script handling is controlled by NoScripts and PreferMHTScripts.Returns true for success, false for failure.
GetAndZipEML
· return:boolean;truefor success,falsefor failure
· url_or_htmlFilepath:string
· zipEntryFilename:string
· zipFilename:string
Creates EML output from the web-page URL or local HTML file in url_or_htmlFilepath and stores it as zipEntryFilename inside the ZIP archive named by zipFilename. The ZIP file is created when it does not exist; otherwise the new entry is appended to the existing archive.
url_or_htmlFilepath is the source URL or HTML file, zipEntryFilename is the filename of the entry inside the ZIP, and zipFilename is the ZIP file on disk. Images and style sheets are embedded in the EML, and scripts are removed.Returns true for success, false for failure.
topGetAndZipMHT
· return:boolean;truefor success,falsefor failure
· url_or_htmlFilepath:string
· zipEntryFilename:string
· zipFilename:string
Creates MHT output from the web-page URL or local HTML file in url_or_htmlFilepath and stores it as zipEntryFilename inside the ZIP archive named by zipFilename. The ZIP file is created when it does not exist; otherwise the new entry is appended to the existing archive.
url_or_htmlFilepath is the source URL or HTML file, zipEntryFilename is the filename of the entry inside the ZIP, and zipFilename is the ZIP file on disk.Returns true for success, false for failure.
topGetCacheRoot
· return:string
· index:int
Returns the cache-root directory at zero-based index index. Cache roots are added with AddCacheRoot, and their count is available from NumCacheRoots.
0 through NumCacheRoots - 1 and check the normal method-success indicator before using the returned string.Returns null on failure
GetEML
· return:string
· url_or_htmlFilepath:string
Creates an EML MIME message from the web-page URL or local HTML file specified by url_or_htmlFilepath and returns the complete EML text in memory. Referenced images and style sheets are embedded as related MIME parts.
GetAndSaveEML avoids keeping the final EML text solely in the return value. Scripts are always removed from EML output.Returns null on failure
GetMHT
· return:string
· url_or_htmlFilepath:string
Creates an MHT web archive from the web-page URL or local HTML file specified by url_or_htmlFilepath and returns the complete MHT MIME text in memory. Referenced resources are embedded according to the current property settings.
GetAndSaveMHT writes the result directly to a file.Returns null on failure
HtmlToEML
· return:string
· htmlText:string
Creates an EML MIME message from the in-memory HTML string in htmlText and returns the complete EML text. External images and style sheets referenced by the HTML are fetched and embedded.
BaseUrl when the HTML contains relative resource references. Scripts are removed from EML output.Returns null on failure
HtmlToEMLFile
· return:boolean;truefor success,falsefor failure
· html:string
· emlFilename:string
Creates an EML email message from the in-memory HTML string in html and saves it to the file path in emlFilename. External images and style sheets are fetched and embedded as related MIME parts.
BaseUrl when the HTML contains relative resource references. Scripts are removed from EML output.Returns true for success, false for failure.
topHtmlToMHT
· return:string
· htmlText:string
Creates an MHT web archive from the in-memory HTML string in htmlText and returns the complete MHT MIME text. Related resources are fetched and embedded according to the current property settings.
BaseUrl when the HTML contains relative resource references. Script handling is controlled by NoScripts and PreferMHTScripts.Returns null on failure
HtmlToMHTFile
· return:boolean;truefor success,falsefor failure
· html:string
· mhtFilename:string
Creates an MHT web archive from the in-memory HTML string in html and saves it to the file path in mhtFilename. Related resources are fetched and embedded according to the current property settings.
BaseUrl when the HTML contains relative resource references.Returns true for success, false for failure.
topRemoveCustomHeader
· return:undefined
· name:string
Removes the custom HTTP request header whose field name is given by name. The name should be supplied without a trailing colon.
ClearCustomHeaders to remove every custom header at once.RestoreDefaults
· return: undefined
Restores the Mht object's configurable property values to their defaults.
ClearCustomHeaders, when the application also needs to remove state that was added through methods rather than properties.UnpackMHT
· return:boolean;truefor success,falsefor failure
· mhtFilename:string
· unpackDir:string
· htmlFilename:string
· partsSubDir:string
Extracts the MHT file in mhtFilename into the directory specified by unpackDir. The main HTML document is written using filename htmlFilename. In normal mode, related files are written beneath the partsSubDir subdirectory, which Chilkat creates when necessary, and the HTML is rewritten to reference those extracted files.
UnpackDirect to preserve relative Content-Location paths without rewriting HTML; in that mode partsSubDir is ignored. UnpackUseRelPaths controls relative versus absolute references in normal mode.Returns true for success, false for failure.
UnpackMHTString
· return:boolean;truefor success,falsefor failure
· mhtString:string
· unpackDir:string
· htmlFilename:string
· partsSubDir:string
Extracts the in-memory MHT text in mhtString. unpackDir specifies the destination directory, htmlFilename specifies the main HTML filename, and partsSubDir specifies the related-parts subdirectory used by normal unpacking. Its extraction behavior is otherwise the same as UnpackMHT.
UnpackDirect is true, partsSubDir is ignored and relative Content-Location paths are used directly beneath the unpack directory.Returns true for success, false for failure.