Streaming live at 10am (PST)

Remember user input & display on all pages

Hello smart people!

I am looking to capture a users name on the first splash screen of a site.

The users name would then output on every page throughout the site.

I am across how to output an input from a form field on the same page, see a simple test in webflow and published.

But I am not sure how to store that input and print it across all pages through the life of the users session.

Any help is greatly appreciated. Thanks in advance!

Hello @damiandp,

If you’re looking to use the input value of a user throughout it’s life session on the website you could use cookies. I wrote a quick script using cookies.js for you. Have a look !

What it basicaly does is on DOM loaded, it checks if there is already a specific cookie set. If it is set, it then targets an id and inject the cookie value inside. If there is no cookie set, then javascript listens on a submit event and sets a cookie once the user has submited the webflow form which will store the username as the cookie value.

Here is a codepen I wrote which illustrate the mechanisme. First, you should just see “hello”, then if you submit a name, and refresh the page, you should see “hello xyz” on that same page or any other page of your website that contains that piece of JavaScript.

Here is a quick screenrecording to showcase the script at work.

document.addEventListener("DOMContentLoaded", () => {
  // globals
  const log = console.log,
    submitBtn = document.getElementById("submit"),
    greetings = document.getElementById("greetings"),
    cookieName = "username-cookie",
    dayStored = 365;

  let date = new Date(),
    cookieValue; // username

  // check if cookie exist
  (() => {
    if (!Cookies.get(cookieName)) {
      // cookie does not exist yet
      log("no username cookie found yet");
      setCookie();
    } else {
      // cookie exists, do something with cookievalue
      greetings.textContent = `${Cookies.get(cookieName)}`;
    } // end if
  })(); // end cookie check IIFE

  // set cookie on click
  function setCookie() {
    // set date to be equal to x amount of days from current date time in ms
    date.setTime(date.getTime() + dayStored * 24 * 60 * 60 * 1000);

    // create cookie on button click to expire on newly defined date
    submitBtn.addEventListener("click", (event) => {
      event.preventDefault();
      cookieValue = document.getElementById("user").value;
      Cookies.set(cookieName, cookieValue, {
        expires: date
      }); // end set cookie
      log(`${cookieValue} was stored as a cookie`);
    }); // end listener
  } // end setCookie()
}); // end DOMloaded

Hope that helps!

1 Like

Incredible @anthonysalamin, this worked perfectly!!!

Thank you so much for the quality response. I truely appreciate you taking the time to help me out :slight_smile:

1 Like