Search.../

to( )

Directs the browser to navigate to the specified URL.

Description

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

The following link patterns are supported:

  • /localPageURL: Another page on your site.
  • /localPageURL#<corvid-element-id>: Another page on your site scrolled to the element with the specified ID. The element must be an element that supports the scrollTo function.
  • /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 (-).

Syntax

function to(url: string): void

to Parameters

NAME
TYPE
DESCRIPTION
url

string

The URL of the page or website to navigate to.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

Navigate to a local link

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("/about-me");
Navigate to a local link and scroll to an element

This example assumes you have a page named Long Page that has an element with the ID downUnder.

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("/long-page#downUnder");
6
Navigate to an external web link

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("http://wix.com");
Open a new email window

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("mailto:a@b.com?subject=Something%20Intersting");
Open a document

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("wix:document://v1/9bec_52fb06ea/filename.xls");
Make a phone call

Code Example

Copy Code
1import wixLocation from 'wix-location';
2
3// ...
4
5wixLocation.to("tel:+1-555-555-5555");