Webflow sync, pageviews & more.
NEW
Answers

Is it possible to make a button on a Webflow page display a random item from a Collection without any page loads using custom Javascript and without exposing sensitive data or API keys to users?

Yes, it is possible to make a button on a Webflow page display a random item from a Collection without any page loads using custom JavaScript and without exposing sensitive data or API keys to users. Here is one way to achieve this:

1. Create a Collection in your Webflow project that contains the items you want to display randomly.

2. Add fields to your Collection that store the information you want to display for each item (e.g., image, title, description).

3. In your Webflow project, create a hidden div or section where you will dynamically display the random item.

4. Write custom JavaScript code to fetch the Collection items and display a random item when the button is clicked. Here's an example using the Webflow API:

```javascript
// Add your Webflow API URL and access token here
const apiUrl = 'https://api.webflow.com/collections/{collectionId}/items';
const accessToken = 'your-access-token';

// Add your Collection ID here
const collectionId = 'your-collection-id';

// Get the button element
const button = document.getElementById('randomButton');

// Add click event listener to the button
button.addEventListener('click', getRandomItem);

// Function to get a random item from the Collection
async function getRandomItem() {
try {
// Fetch the Collection items using the Webflow API
const response = await fetch(`${apiUrl}/${collectionId}`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});

// Parse the response as JSONconst data = await response.json();// Display a random item from the Collectionconst randomIndex = Math.floor(Math.random() \* data.items.length);const randomItem = data.items[randomIndex];// Update the hidden div or section with the random item's informationconst itemTitle = document.getElementById('itemTitle');const itemDescription = document.getElementById('itemDescription');const itemImage = document.getElementById('itemImage');itemTitle.textContent = randomItem.fields.title;itemDescription.textContent = randomItem.fields.description;itemImage.src = randomItem.fields.image;// Show the hidden div or section// Replace 'hidden-div' with the ID or class of your hidden elementdocument.querySelector('.hidden-div').style.display = 'block';

} catch (error) {
console.log(error);
}
}
```

To protect sensitive data, you can use environment variables to store your API URL and access token instead of hardcoding them in the JavaScript code. This way, you can keep your API credentials secure.

Remember to replace `'your-access-token'`, `'your-collection-id'`, and `'hidden-div'` placeholders in the code with the appropriate values from your Webflow project.

This approach allows you to display a random item from your Collection on your Webflow page without any page loads, and it keeps your sensitive data and API keys hidden from users.

Rate this answer

Other Webflow Questions