to( )


Directs the browser to navigate to the specified URL.

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

The to() function can only be used when browser rendering happens, meaning you can only use it in frontend code after the page is ready.

The following link patterns are supported:

  • /localPageURL: Another page on your site.
  • /localPageURL#: 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)://: An external web address.
  • wix:document://: A document stored in the Media Manager.
  • mailto:@?subject=: An email.
  • tel:: 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.

By default, when navigating to a new URL for a Wix page, the page scrolls to the top. Set the disableScrollToTop navigation parameter property to true if you want the page to remain at the current Y-axis position as the previously-viewed page.

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 (-).

Note: The to() function does not work while previewing your site.

Method Declaration
Copy
Method Parameters
urlstringRequired

The URL of the page or website to navigate to.


optionsNavOptions

Options to use when navigating to the specified URL, such as scrolling options.

Did this help?