Class URL
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The URL interface is used to parse, construct, normalize, and encode URL. It works by providing properties which allow you to easily read and modify the components of a URL.
[Value("URL")]
public class URL
- Inheritance
-
URL
- Inherited Members
Remarks
You normally create a new URL object by specifying the URL as a string when calling its constructor, or by providing a relative URL and a base URL. You can then easily read the parsed components of the URL or make changes to the URL.
-Polyfill of URL in core-js
-URL API
-What is a URL?
-URLSearchParams.
Constructors
URL()
public URL()
URL(string, string)
The URL() constructor returns a newly created URL object representing the URL defined by the parameters.
public URL(string url, string base_ = null)
Parameters
Remarks
If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown.
-Parse(string, string), a non-throwing alternative to this constructor
-Polyfill of URL in core-js
-The interface it belongs to: URL.
Properties
Hash
The hash property of the URL interface is a string containing a "#" followed by the fragment identifier of the URL. If the URL does not have a fragment identifier, this property contains an empty string, "".
[Value("hash")]
public string Hash { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the fragment identifier of the URL. When setting, a single "#" prefix is added to the provided value, if not already present. Setting it to "" removes the fragment identifier.
The fragment is Percent-encoding when setting but not percent-decoded when reading.
-The URL interface it belongs to.
Host
The host property of the URL interface is a string containing the host, which is the Hostname, and then, if the {{glossary("port")}} of the URL is nonempty, a ":", followed by the Port of the URL. If the URL does not have a hostname, this property contains an empty string, "".
[Value("host")]
public string Host { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change both the hostname and the port of the URL. If the URL's scheme is not hierarchical (which the URL standard calls "special schemes"), then it has no concept of a host and setting this property has no effect.
NOTE
If the given value for the
hostsetter lacks aport, the URL'sportwill not change. This can be unexpected as thehostgetter does return a URL-port string, so one might have assumed the setter to always "reset" both.
-The URL interface it belongs to.
Hostname
The hostname property of the URL interface is a string containing either the {{glossary("domain name")}} or {{glossary("IP address")}} of the URL. If the URL does not have a hostname, this property contains an empty string, "". IPv4 and IPv6 addresses are normalized, such as stripping leading zeros, and domain names are converted to IDN.
[Value("hostname")]
public string Hostname { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the hostname of the URL. If the URL's scheme is not hierarchical (which the URL standard calls "special schemes"), then it has no concept of a host and setting this property has no effect.
-The URL interface it belongs to.
Href
The href property of the URL interface is
a string containing the whole URL.
[Value("href")]
public string Href { get; set; }
Property Value
- string
A string.
Remarks
-The URL interface it belongs to.
Origin
The origin read-only property of the URL interface returns a string containing the Unicode serialization of the origin of the represented URL.
[Value("origin")]
public string Origin { get; }
Property Value
- string
A string.
Remarks
The exact structure varies depending on the type of URL:
For all other cases, the string "null" is returned.
Password
The password property of the URL interface is a string containing the password component of the URL. If the URL does not have a password, this property contains an empty string, "".
[Value("password")]
public string Password { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the password of the URL. If the URL has no Host or its scheme is file:, then setting this property has no effect.
The password is Percent-encoding when setting but not percent-decoded when reading.
-The URL interface it belongs to.
Pathname
The pathname property of the URL interface represents a location in a hierarchical structure. It is a string constructed from a list of path segments, each of which is prefixed by a / character.
[Value("pathname")]
public string Pathname { get; set; }
Property Value
- string
A string.
Remarks
HTTPS, HTTP, or other URLs with hierarchical schemes (which the URL standard calls "special schemes") always have at least one (invisible) path segment: the empty string.
The pathname value for such URLs will therefore always have at least one / character.
For non-hierarchical schemes, if the URL has no path segments, the value of its pathname property will be the empty string.
-The URL interface it belongs to.
Port
The port property of the URL interface is a string containing the port number of the URL. If the port is the default for the protocol (80 for ws: and http:, 443 for wss: and https:, and 21 for ftp:), this property contains an empty string, "".
[Value("port")]
public string Port { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the port of the URL. If the URL has no Host or its scheme is file:, then setting this property has no effect. It also silently ignores invalid port numbers.
-The URL interface it belongs to.
Protocol
The protocol property of the URL interface is a string containing the protocol or scheme of the URL, including the final ":".
[Value("protocol")]
public string Protocol { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the protocol of the URL. A ":" is appended to the provided string if not provided. The provided scheme has to be compatible with the rest of the URL to be considered valid.
-The URL interface it belongs to.
Search
The search property of the URL interface is a search string, also called a query string, that is a string containing a "?" followed by the parameters of the URL. If the URL does not have a search query, this property contains an empty string, "".
[Value("search")]
public string Search { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the query string of the URL. When setting, a single "?" prefix is added to the provided value, if not already present. Setting it to "" removes the query string.
The query is Percent-encoding when setting but not percent-decoded when reading.
Modern browsers provide the SearchParams property to make it easy to
parse out the parameters from the query string.
-The URL interface it belongs to.
SearchParams
The searchParams read-only property of the
URL interface returns a URLSearchParams object allowing
access to the GET decoded query arguments contained in the URL.
[Value("searchParams")]
public URLSearchParams SearchParams { get; }
Property Value
- URLSearchParams
A URLSearchParams object.
Remarks
Username
The username property of the URL interface is a string containing the username component of the URL. If the URL does not have a username, this property contains an empty string, "".
[Value("username")]
public string Username { get; set; }
Property Value
- string
A string.
Remarks
This property can be set to change the username of the URL. If the URL has no Host or its scheme is file:, then setting this property has no effect.
The username is Percent-encoding when setting but not percent-decoded when reading.
-The URL interface it belongs to.
Methods
CanParse(string, string)
The URL.canParse() static method of the URL interface returns a boolean indicating whether or not an absolute URL, or a relative URL combined with a base URL, are parsable and valid.
[Value("canParse")]
public static bool CanParse(string url, string base_ = null)
Parameters
Returns
- bool
trueif the URL can be parsed and is valid;falseotherwise.
Remarks
This is a fast and easy alternative to constructing a URL within a try...catch block.
It returns true for the same values for which the URL() constructor would succeed, and false for the values that would cause the constructor to throw.
-URL(string, string)
-A polyfill of URL.canParse() is available in core-js
CreateObjectURL(Union46)
The createObjectURL() static method of the URL interface
creates a string containing a blob URL pointing to the object given in the parameter.
[Value("createObjectURL")]
public static string CreateObjectURL(Union46 obj)
Parameters
objUnion46
Returns
- string
A string containing an object URL that can be used to reference the
contents of the specified sourceobject.
Remarks
For more information, see blob URLs.
To release an object URL, call RevokeObjectURL(string).
NOTE
This feature is not available in Service Workers due to its
potential to create memory leaks.
-Blob URLs
-Using files from web applications
-Using object URLs to display images
-RevokeObjectURL(string)
-SrcObject
-ReadAsDataURL(Blob)
Parse(string, string)
The URL.parse() static method of the URL interface returns a newly created URL object representing the URL defined by the parameters.
[Value("parse")]
public static URL? Parse(string url, string base_ = null)
Parameters
Returns
- URL
A
URLif the parameters can be resolved to a valid URL;nullotherwise.
Remarks
If the given base URL or the resulting URL are not parsable and valid URLs, null is returned.
This is an alternative to using the URL(string, string) constructor to construct a URL within a try...catch block, or using CanParse(string, string) to check the parameters and returning null if the method returns false.
-URL() constructor, which throws if the passed parameters define an invalid URL
-A polyfill of URL.parse() is available in core-js
RevokeObjectURL(string)
The revokeObjectURL() static method of the URL interface
releases an existing object URL which was previously created by calling
CreateObjectURL(Union46).
[Value("revokeObjectURL")]
public static GlobalObject.Undefined RevokeObjectURL(string url)
Parameters
urlstring
Returns
Remarks
For more information, see blob URLs.
Call this method when you've finished
using an object URL to let the browser know not to keep the reference to the file any
longer.
NOTE
This method is not available in Service Workers, due to
issues with the Blob interface's life cycle and the potential for leaks.
-Blob URLs
-Using files from web applications
-Using object URLs to display images
-CreateObjectURL(Union46)
ToJSON()
The toJSON() method of the URL interface
returns a string containing a serialized version of the URL,
although in practice it seems to have the same effect as
URL.ToString.
[Value("toJSON")]
public string ToJSON()
Returns
- string
A string.