CorvidReference

wix-location

The wix-location module contains functionality for getting information about the URL of the current page and for navigating to other pages.

To use the location module, import wixLocation from the wix-location module:

   import wixLocation from 'wix-location';

The APIs in wix-location can only be used in front-end code and only after the page is ready.

Premium Sites

Given the premium site URL: https://domain.com/animals/mammals/elephant?species=african-elephant, the URL is broken down as shown in the table below:

url
baseUrl
protocolhostprefixpathquery
https://domain.com/animals/mammals/elephant?species=african-elephant

Free Site

Given the free site URL: https://user.wix-sites.com/zoo/animals/mammals/elephant?species=african-elephant, the URL is broken down as shown in the table below:

url
baseUrl
protocolhostsiteprefixpathquery
https://user.wix-sites.com/zoo/animals/mammals/elephant?species=african-elephant

Table of Contents

PROPERTIES

?
Store values associated with an object.
baseUrlGets the base URL of the page.
pathGets the path of the page's URL.
prefixGets the prefix of a dynamic page's or router page's URL.
protocolGets the protocol of the page's URL.
queryGets an object that represents the query segment of the page's URL.
urlGets the full URL of the current page.

FUNCTIONS

?
Perform actions on an object.
to( )Directs the browser to navigate to the specified URL.

Related Content

FAQ

    Do I use wix-location to find the physical location of a site visitor?
    How can I open a link in a new tab when using the 'to()' function?
    How do I pass the query from a page's URL on to the next page I direct the browser to?
    How do I remove the query parameters that are kept when I navigate between pages in my site?

baseUrl

Gets the base URL of the page.

Description

Premium sites:

Free sites:

If the site is not yet published, the function returns null.

See Also

url

Syntax

get baseUrl(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the base URL of the current page

import wixLocation from 'wix-location';

// ...

let baseUrl = wixLocation.baseUrl;
// Premium site: "https://domain.com/"
// Free site: "https://user.wix-sites.com/zoo/"

path

Gets the path of the page's URL.

Description

Premium sites:

Free sites:

Syntax

get path(): Array<string>
TYPE
?
The kind of data the property stores.
Array<string>

Examples

Get the path of the current page's URL

import wixLocation from 'wix-location';

// ...

let path = wixLocation.path; // ["mammals", "elephant"]

prefix

Gets the prefix of a dynamic page's or router page's URL.

Description

Only dynamic pages and router pages have a prefix. The value of the prefix property for other page types is always undefined.

Premium sites:

Free sites:

To learn more about dynamic page prefixes, see About URL Prefixes and Page Grouping of Dynamic Pages.

To learn more about router page prefixes, see About Routers.

Syntax

get prefix(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the prefix of the current page's URL

import wixLocation from 'wix-location';

// ...

let prefix = wixLocation.prefix; // "animals"

protocol

Gets the protocol of the page's URL.

Description

Premium sites:

Free sites:

Syntax

get protocol(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the protocol of the current page's URL

import wixLocation from 'wix-location';

// ...

let protocol = wixLocation.protocol; // "https"

query

Gets an object that represents the query segment of the page's URL.

Description

Premium sites:

Free sites:

Syntax

get query(): Object
TYPE
?
The kind of data the property stores.
Object

Examples

Get the query of the current page's URL

import wixLocation from 'wix-location';

// ...

let query = wixLocation.query; // {"species": "african-elephant"}

url

Gets the full URL of the current page.

Description

Premium sites:

Free sites:

If the site is not yet published, the function returns null.

See Also

baseUrl

Syntax

get url(): string
TYPE
?
The kind of data the property stores.
string

Examples

Get the full URL of the current page

import wixLocation from 'wix-location';

// ...

let url = wixLocation.url;
// Premium site: "https://domain.com/animals/mammals/elephant?species=african-elephant#desc"
// Free site: "https://user.wix-sites.com/zoo/animals/mammals/elephant?species=african-elephant#desc"

to( )

Directs the browser to navigate to the specified URL.

Description

The to() function navigates the browser to another web page.

to() supports the following link patterns:

  • /localPageURL: another page on your site
  • /localPageURL?queryParam=value: another page on your site with query parameters
  • /: your site's home page
  • http(s)://<url>: an external web address
  • wix:document://<location>: a document stored in the Media Manager
  • mailto:<address>@<someplace.com>?subject=<subject>: an email
  • tel:<phone number>: a phone number

To find the local URL of a page on your site in the Editor:

  • Regular page: See the SEO tab of the Page Settings panel.
  • Dynamic page: See the Page Info tab of the Page Settings panel for the URL structure. The actual URL used for navigation needs to contain values where the placeholders are.

    For example, if the URL structure of your dynamic page looks like:

    and you have an item with the title "Waffles", the local URL to that page is /Recipes/Waffles.

  • Router page: You cannot navigate directly to a specific router page. You can navigate to a URL with the router's prefix and the router code decides which page to route to.

The to() function attempts to properly encode the URL parameter that is passed to it. For example, .../some page is encoded to .../some%20page. However, some URLs do not have one unambiguous encoding. In those cases it is up to you to encode the URL to reflect your intentions. Because of these situations, it is a best practice to always encode URLs before you pass them to the to() function.

Note that Wix URLs do not contain spaces. A page which has spaces in its name has its spaces replaced with dashes (-). Similarly, a dynamic page whose URL contains the value of a field in your collection with spaces has its spaces replaced with dashes (-).

Syntax

function to(url: string): void
PARAMETERS
?
Values that you pass to a function.
url
string
The URL of the page or website to navigate to.

Examples

Navigate to a local link

import wixLocation from 'wix-location';

// ...

wixLocation.to("/about-me");

Navigate to an external web link

import wixLocation from 'wix-location';

// ...

wixLocation.to("http://wix.com");

Open a new email window

import wixLocation from 'wix-location';

// ...

wixLocation.to("mailto:a@b.com?subject=Something%20Intersting");

Open a document

import wixLocation from 'wix-location';

// ...

wixLocation.to("wix:document://v1/9bec_52fb06ea/filename.xls");

Make a phone call

import wixLocation from 'wix-location';

// ...

wixLocation.to("tel:+1-555-555-5555");