Skip to content

Available Actions for Global Events

  • Perform a Request: Executes a specific request to an API. You can add an optional condition using the Function Editor to control when the request is executed. For example, you can chain multiple requests, executing one only if the previous one was successful.

  • Set Cookie: Updates the value of an existing cookie or creates a new one. You can use the Function Editor to define the new value of the cookie and add optional conditions.

  • Set Variable: Updates the value of an existing variable or creates a new one. You can use the Function Editor to define the new value of the variable and add optional conditions.

  • Navigate to: Redirects the user to another URL or route within your application. You can add an optional condition using the Function Editor to control when the redirection occurs. For example, you can redirect the user to a login page if they are not authenticated.

  • Run Function: Executes a custom JavaScript code snippet. You can use this action to perform complex tasks or programmatically interact with elements on your page. For example:

Examples and Use Cases

1. Perform a Request

This action allows you to send a request to an external API in response to a global event. You can use it to fetch updated data, send information to a server, or perform any other operation that requires communicating with an external service.

Example:

  • Event: Page Load Complete
  • Action: Perform a Request to get user profile data from an API.
  • Configuration:
    • Request: Select the API request you want to execute (e.g., getUserData).
    • Condition (optional): You can add a condition in the Function Editor to control when the request is executed. For example, only if the user is authenticated:
javascript
// Check if there is an authenticated user
return c.loggedUser;

This action allows you to create a new cookie or update the value of an existing one. Cookies are useful for storing information in the user's browser, such as preferences, session data, or authentication tokens.

Example:

  • Event: Request Completed (associated with a login request)
  • Condition: r.loginRequest.ok (True if the login was successful)
  • Action: Set Cookie to create a cookie named tokenUsuario with the value of the authentication token received from the API:
javascript
// Returns the authentication token from the login
return r.loginRequest.data.token;

3. Navigate to

This action allows you to redirect the user to another page or URL within your application or to an external site. You can use it to create custom navigation flows or send the user to a specific page after completing an action.

Example:

  • Event: Request Completed (associated with a user registration request)
  • Condition: r.registroUsuario.ok (True if the registration was successful)
  • Action: Navigate to redirect the user to the login page:
javascript
// Returns the URL of the login page
return '/login';

4. Run Function

This action allows you to execute a custom JavaScript code snippet in response to a global event. It is useful for performing complex tasks, interacting with DOM elements, or implementing logic that cannot be achieved with predefined actions.

Example:

  • Event: Page Load Start
  • Action: Run Function to initialize an external JavaScript library and configure it:
javascript
// Make sure to have imported the library in your Webflow project
return initializeMyLibrary();