navigateTo(target, options)

Sets the current page.

Copy
Scrivito.navigateTo(
  () => Scrivito.Obj.getByPermalink("blog"), 
  { hash: "my-hash", params: { foo: "bar" }}
);

After a call to navigateTo,

  • currentPage reflects the new page,
  • The browser location and navigation history are updated accordingly,
  • The window scroll position is set to “top”.

Please note that the changes may be applied asynchronously.

Params

  • target (Function, Link, Obj) – The page or resource to be opened. This can be a callback function returning an Obj or Link.
  • options (Object) – Supports setting the hash and the query parameters of the browser location.
    • hash (String) – The browser location hash
    • params (Object) – The browser location query parameters

Throws

  • ArgumentError if the target parameter is invalid or missing.

Remarks

  • If the target is an external link, Scrivito doesn't change the currentPage and the window scroll position.
  • If the target is a Link, the hash and query of the link are used as navigateTo options.
  • If you call navigateTo multiple times, the last call (in the order in which they were made) wins.
  • Scrivito loads data asynchronously from the CMS backend. To make sure your target object data is available for navigation, pass in target as a function.
  • For convenience, navigateTo offers a shortcut for providing query parameters: unknown keys in the options object are interpreted as query parameters.
  • A callback function will always work:
    Scrivito.navigateTo(() => Scrivito.Obj.getByPath("/company/about-us"));
    This fails if "About Us" has not yet been loaded in the current page context:
    Scrivito.navigateTo(Scrivito.Obj.getByPath("/company/about-us"));

Examples

Copy
// Scroll to top
Scrivito.navigateTo(Scrivito.currentPage());

// Navigate to "/myPageId?search=foo&extended=1"
Scrivito.navigateTo(myPage, { params: { search: 'foo', extended: 1 }});

// Navigate to "/myPageId?search=foo&extended=1"
Scrivito.navigateTo(myPage, { search: 'foo', extended: 1 });

// Navigate to "/myPageId#spam"
Scrivito.navigateTo(myPage, { hash: 'spam' });