If you've ever wanted to add a lightweight AI utility directly into a static website without heavy backend frameworks, a pure JavaScript approach using a fetch request to an LLM endpoint is a great option.
In this quick walkthrough, let's look at how to set up a clean, minimal interface that takes text input and processes it via an API.
- The Markup (HTML) Keep the layout clean with a text area for input, a trigger button, and a response container:
HTML
Quick AI Summarizer
Process Text
- The Logic (JavaScript) Using modern vanilla JS, we can handle the asynchronous click event and render the output dynamically:
JavaScript
document.getElementById('runBtn').addEventListener('click', async () => {
const text = document.getElementById('inputText').value;
const outputDiv = document.getElementById('outputResult');
if (!text.trim()) {
outputDiv.innerText = "Please enter some text first.";
return;
}
outputDiv.innerText = "Processing...";
try {
// Simulated or actual API endpoint integration
// Replace with your endpoint logic
const response = await simulateAIProcess(text);
outputDiv.innerText = response;
} catch (err) {
outputDiv.innerText = "Error processing request.";
}
});
function simulateAIProcess(input) {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Summary: " + input.substring(0, 100) + "...");
}, 1000);
});
}
Wrapping Up
This pattern keeps client-side tools fast and responsive. If you need a fully custom AI web application, online utility, or complete dashboard built out for your project without bloated dependencies, you can check out my AI Website / Web App Gig on Fiverr.
Top comments (0)