wix-location

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:

ICAgaW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7
   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
protocol host prefix path query
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
protocol host site prefix path query
https :// user.wix-sites.com / zoo /animals /mammals/elephant ? species=african-elephant

Contents

baseUrl Gets the base URL of the page.
path Gets the path of the page's URL.
prefix Gets the prefix of a dynamic page's or router page's URL.
protocol Gets the protocol of the page's URL.
query Gets an object that represents the query segment of the page's URL.
url Gets the full URL of the current page.
to( ) Directs the browser to navigate to the specified URL.
baseUrl

baseUrl

Gets the base URL of the page.

Syntax

get baseUrl(): string

Description

Premium sites: Premium site baseUrl

Free sites: Free site baseUrl

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

Type

String

Examples

Get the base URL of the current page

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCBiYXNlVXJsID0gd2l4TG9jYXRpb24uYmFzZVVybDsKLy8gUHJlbWl1bSBzaXRlOiAiaHR0cHM6Ly9kb21haW4uY29tLyIKLy8gRnJlZSBzaXRlOiAiaHR0cHM6Ly91c2VyLndpeC1zaXRlcy5jb20vem9vLyIK
import wixLocation from 'wix-location';

// ...

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

See Also

url

path

path

Gets the path of the page's URL.

Syntax

get path(): string[]

Description

Premium sites: Premium site path

Free sites: Free site path

Type

String[ ]

Examples

Get the path of the current page's URL

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCBwYXRoID0gd2l4TG9jYXRpb24ucGF0aDsgLy8gWyJtYW1tYWxzIiwgImVsZXBoYW50Il0K
import wixLocation from 'wix-location';

// ...

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

prefix

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

Syntax

get prefix(): string

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: Premium site prefix

Free sites: Free site prefix

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.

Type

String

Examples

Get the prefix of the current page's URL

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCBwcmVmaXggPSB3aXhMb2NhdGlvbi5wcmVmaXg7IC8vICJhbmltYWxzIgo=
import wixLocation from 'wix-location';

// ...

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

protocol

Gets the protocol of the page's URL.

Syntax

get protocol(): string

Description

Premium sites: Premium site protocol

Free sites: Free site protocol

Type

String

Examples

Get the protocol of the current page's URL

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCBwcm90b2NvbCA9IHdpeExvY2F0aW9uLnByb3RvY29sOyAvLyAiaHR0cHMiCg==
import wixLocation from 'wix-location';

// ...

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

query

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

Syntax

get query(): Object

Description

Premium sites: Premium site query

Free sites: Free site query

Type

Object

Examples

Get the query of the current page's URL

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCBxdWVyeSA9IHdpeExvY2F0aW9uLnF1ZXJ5OyAvLyB7InNwZWNpZXMiOiAiYWZyaWNhbi1lbGVwaGFudCJ9Cg==
import wixLocation from 'wix-location';

// ...

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

url

Gets the full URL of the current page.

Syntax

get url(): string

Description

Premium sites: Premium site URL

Free sites: Free site URL

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

Type

String

Examples

Get the full URL of the current page

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCmxldCB1cmwgPSB3aXhMb2NhdGlvbi51cmw7Ci8vIFByZW1pdW0gc2l0ZTogImh0dHBzOi8vZG9tYWluLmNvbS9hbmltYWxzL21hbW1hbHMvZWxlcGhhbnQ/c3BlY2llcz1hZnJpY2FuLWVsZXBoYW50I2Rlc2MiCi8vIEZyZWUgc2l0ZTogImh0dHBzOi8vdXNlci53aXgtc2l0ZXMuY29tL3pvby9hbmltYWxzL21hbW1hbHMvZWxlcGhhbnQ/c3BlY2llcz1hZnJpY2FuLWVsZXBoYW50I2Rlc2MiCg==
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"

See Also

baseUrl

to( )

to( )

Directs the browser to navigate to the specified URL.

function to(url: string): void

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: Dynamic Page URL 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 (-).

Parameters

url String The URL of the page or website to navigate to.

Examples

Navigate to a local link

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCndpeExvY2F0aW9uLnRvKCIvYWJvdXQtbWUiKTsK
import wixLocation from 'wix-location';

// ...

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

Navigate to an external web link

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCndpeExvY2F0aW9uLnRvKCJodHRwOi8vd2l4LmNvbSIpOwo=
import wixLocation from 'wix-location';

// ...

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

Open a new email window

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCndpeExvY2F0aW9uLnRvKCJtYWlsdG86YUBiLmNvbT9zdWJqZWN0PVNvbWV0aGluZyUyMEludGVyc3RpbmciKTsK
import wixLocation from 'wix-location';

// ...

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

Open a document

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCndpeExvY2F0aW9uLnRvKCJ3aXg6ZG9jdW1lbnQ6Ly92MS85YmVjXzUyZmIwNmVhL2ZpbGVuYW1lLnhscyIpOwo=
import wixLocation from 'wix-location';

// ...

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

Make a phone call

aW1wb3J0IHdpeExvY2F0aW9uIGZyb20gJ3dpeC1sb2NhdGlvbic7CgovLyAuLi4KCndpeExvY2F0aW9uLnRvKCJ0ZWw6KzEtNTU1LTU1NS01NTU1Iik7Cg==
import wixLocation from 'wix-location';

// ...

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