How to Add JavaScript to Squarespace for Dynamic Functionality



Affiliate disclosure: As an Amazon Associate, we may earn commissions from qualifying purchases

JavaScript is a programming language that can be used to add dynamic, interactive elements to websites. With JavaScript, you can create features like countdown timers, contact forms, lightbox galleries, and live social media feeds on your Squarespace website. While Squarespace has some built-in functionality, JavaScript allows you to customize and enhance your site even further. In this article, we’ll walk through the two main methods for adding JavaScript to Squarespace, along with some examples of JavaScript code you can use.

How to Add JavaScript to Squarespace

There are two primary ways to add custom JavaScript code to your Squarespace site:

Using the Code Injection Tool

The easiest way to add JavaScript to Squarespace is through the Code Injection tool in the Site Manager. Here’s how:

  1. Log in to your Squarespace Site Manager.
  2. Go to the Design menu and select Code Injection.
  3. Choose “Footer” to add your code before the closing tag.
  4. Paste your JavaScript code into the text box.
  5. Click Save.

The code will now be inserted across all pages of your site.

Using the Code Block

You can also add JavaScript to specific pages or blocks using the Code Block element:

  1. Edit the page or block where you want the code to appear.
  2. Drag the Code Block element onto the page.
  3. Paste your JavaScript code into the Code Block text area.
  4. Click Save.

The JavaScript will only apply to that particular page or block.

Examples of JavaScript You Can Use on Squarespace

Here are some examples of JavaScript you can add to your Squarespace site to create dynamic features:

Countdown Timer

A countdown timer is great for launching a new product, event, or promotion:

// Set launch date
var countDownDate = new Date("March 1, 2023 00:00:00").getTime();

// Update countdown every 1 second
var x = setInterval(function() {

  // Get current date and time
  var now = new Date().getTime();

  // Calculate time remaining
  var distance = countDownDate - now;

  // Display countdown
  document.getElementById("timer").innerHTML = Math.floor(distance / (1000 * 60 * 60 * 24)) + "d " + Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + "h "
  + Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)) + "m " + Math.floor((distance % (1000 * 60)) / 1000) + "s ";

  // If countdown complete, show expired
  if (distance < 0) {
    document.getElementById("timer").innerHTML = "Expired";
}, 1000);

Contact Form

Allow visitors to contact you directly from your site:

// Listen for form submit
document.getElementById("contactForm").addEventListener("submit", function(event) {

  // Prevent default submit

  // Get form data
  var name = document.getElementById("nameInput").value;
  var email = document.getElementById("emailInput").value;
  var message = document.getElementById("messageInput").value;

  // Send data to backend
  sendContactForm(name, email, message); 


// Function to send data to backend
function sendContactForm(name, email, message) {
  // Send AJAX request

Lightbox Gallery

Display images in a popup overlay:

// Get all image elements
var images = document.getElementsByTagName("img");

// Loop through images  
for(var i = 0; i < images.length; i++) {

  // Listen for click
  images[i].addEventListener("click", function(event) {

    // Get clicked image source
    var imageSrc =; 

    // Create lightbox overlay
    var lightbox = document.createElement("div"); = "lightbox";

    // Display large image 
    var largeImage = document.createElement("img");
    largeImage.src = imageSrc;

    // Close on click
    lightbox.onclick = function() {



Social Media Feed

Embed live social content from Instagram, Twitter, YouTube, etc.

// Initialize Instagram feed
var instagramFeed = new Instafeed({
  accessToken: 'YOUR_ACCESS_TOKEN',
  target: 'instagram-feed',
  limit: 6

// Display images;

Troubleshooting Tips for JavaScript in Squarespace

Here are some common troubleshooting tips for using JavaScript in Squarespace:

  • Check for JavaScript errors in the browser console. Fix any syntax errors.
  • Make sure your code is pasted in the correct location – the footer for site-wide code or in a Code Block for page specific code.
  • Wrap your code in a $(document).ready() function to ensure it runs after the page loads.
  • If using AJAX, enable CORS on your server API to allow cross-origin requests.
  • Use browser developer tools to debug your JavaScript code.


Adding JavaScript to Squarespace provides endless possibilities for creating unique, dynamic sites. With just a few lines of code, you can build custom features beyond Squarespace’s built-ins. Start experimenting with some of the examples listed above to take your Squarespace site to the next level. The process is straightforward whether you use the Code Injection tool or Code Blocks. Refer to the troubleshooting tips if you run into any issues getting your JavaScript to work properly.

Leave a Comment



Join our email list to receive the latest updates.