Skip to content
  • Auto
  • Light
  • Dark
FeedbackDashboard
Customization

Custom JavaScript

Customization

Add JavaScript to your site to enable dynamic functionality, telemetry, and more.

To add a script to your entire site, use the head property in your astro.config.ts file to add the script to your site’s <head> element.

First, create a new JavaScript file in your project’s public directory:

public/myGlobalScript.js
console.log('Hello from my global script!');

Next, add the script to your site’s <head> in your astro.config.ts file:

astro.config.ts
import { defineConfig } from "astro/config";
import { stainlessDocs } from "@stainless-api/docs";
export default defineConfig({
integrations: [
stainlessDocs({
title: "My Docs",
head: [
{
tag: "script",
attrs: {
src: "global.js",
},
},
],
}),
],
});

Your script will now be run every time any page of your site loads.

We recommend keeping your JavaScript in separate files, as outlined above, but you can provide JavaScript directly in astro.config.ts if you wish.
astro.config.ts
import { defineConfig } from "astro/config";
import { stainlessDocs } from "@stainless-api/docs";
export default defineConfig({
integrations: [
stainlessDocs({
title: "My Docs",
head: [
{
tag: "script",
content: `console.log('Hello from my inline script!');`,
},
],
}),
],
});

Stainless Docs is built on top of Astro, so you can add scripts to individual pages of your site using the approach in Astro’s client-side scripts documentation.