Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

url

Experimental

Construct, validate, and parse URLs.

Globals

Constructors

URL(source, base)

The URL constructor creates an object that represents a URL, verifying that the provided string is a valid URL in the process. Any API in the SDK which has a URL parameter will accept URL objects, not raw strings, unless otherwise noted.

Parameters

source : string
A string to be converted into a URL. If source is not a valid URI, this constructor will throw an exception.

base : string
An optional string used to resolve relative source URLs into absolute ones.

DataURL(uri)

The DataURL constructor creates an object that represents a data: URL, verifying that the provided string is a valid data: URL in the process.

Parameters

uri : string
A string to be parsed as Data URL. If is not a valid URI, this constructor will throw an exception.

Functions

toFilename(url)

Attempts to convert the given URL to a native file path. This function will automatically attempt to resolve non-file protocols, such as the resource: protocol, to their place on the file system. An exception is raised if the URL can't be converted; otherwise, the native file path is returned as a string.

Parameters

url : string
The URL, as a string, to be converted.

Returns

string : The converted native file path as a string.

fromFilename(path)

Converts the given native file path to a file: URL.

Parameters

path : string
The native file path, as a string, to be converted.

Returns

string : The converted URL as a string.

isValidURI(uri)

Checks the validity of a URI. isValidURI("https://mozilla.org") would return true, whereas isValidURI("mozilla.org") would return false.

Parameters

uri : string
The URI, as a string, to be tested.

Returns

boolean : A boolean indicating whether or not the URI is valid.

getTLD(url)

Returns the top-level domain for the given URL: that is, the highest-level domain under which individual domains may be registered. Uses getPublicSuffix() internally.

var urls = require("sdk/url");
console.log(urls.getTLD("https://www.bbc.co.uk/"));          // "co.uk"
console.log(urls.getTLD("https://developer.mozilla.org/")); // "org"
Parameters

url : string
The URL, as a string.

Returns

string : The top-level domain for the URL.

URL

Methods

toString()

Returns a string representation of the URL.

Returns

string : The URL as a string.

toJSON()

Returns a string representation of the URL.

Returns

string : The URL as a string.

Properties

scheme

The name of the protocol in the URL, without the trailing ':'. Compare with protocol. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.scheme);  // https

userPass

The username:password part of the URL, null if not present.

host

The host of the URL, null if not present. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.host);  // developer.mozilla.org

port

The port number of the URL, null if none was specified.

path

The path component of the URL. Contains everything after the host of the URL. Compare with pathname. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.path);  // /en-US/Add-ons?example=true&visible=yes#top

hostname

The domain of the URL, as a string. Mirrors window.location.hostname. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.hostname);  // developer.mozilla.org

pathname

An initial '/' followed by the path of the URL, as a string. Compare with path. Mirrors window.location.pathname. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.pathname);  // /en-US/Add-ons

hash

A '#' followed by the fragment identifier of the URL, as a string. Mirrors window.location.hash. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.hash);  // #top

href

The whole URL as a string. Mirrors window.location.href.

origin

The canonical form of the origin for this URL, as a string. Mirrors window.location.origin.

protocol

The protocol of the URL, including the final ':', as a string. Compare with scheme. Mirrors window.location.protocol. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.protocol);  // https:

If any parameters are present, a '?' followed by the parameters of the URL, as a string. Mirrors window.location.search. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons?example=true&visible=yes#top");
console.log(url.search);  // ?example=true&visible=yes

DataURL

Methods

toString()

Returns a string representation of the Data URL. If base64 is true, the data property is encoded using base-64 encoding.

Returns

string : The URL as a string.

Properties

mimeType

The MIME type of the data. By default is an empty string.

parameters

An hashmap that contains the parameters of the Data URL. By default is an empty object.

base64

Defines the encoding for the value in data property.

data

The string that contains the data in the Data URL. If the uri given to the constructor contains base64 parameter, this string is decoded.

Document Tags and Contributors

Tags: 
 Contributors to this page: Mirisong, wbamberg, DD0101, evold
 Last updated by: Mirisong,