url

by 2 contributors:

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("http://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("http://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.

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");
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. For example:

var url = require("sdk/url").URL("https://developer.mozilla.org/en-US/Add-ons");
console.log(url.path);  // /en-US/Add-ons

hostname

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

pathname

An initial '/' followed by the path of the URL, as a string. Mirrors window.location.pathname.

hash

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

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 scheme of the URL, including the final ':', as a string. Mirrors window.location.protocol.

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

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: evold, wbamberg
Last updated by: evold,
Hide Sidebar