Jump to content

Javascript Placement Question

Go to solution Solved by paul2009,

Recommended Posts

Hi

In terms of optimising site speed, where is it best to place a javascript that only applies to functionality on a particular page?

Should it be placed in the page's header code injection area OR should it be placed in the site wide footer code injection area?

My understanding is that placing Javascripts in the footer is best practice as that stops slow loading of the page.

But individual Squarespace pages do not have their own footer code injection area. 

Would appreciate some advice in this matter. 

Thank you

Akanksha

Link to comment
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

  • Solution
2 hours ago, AkankshaK said:

In terms of optimising site speed, where is it best to place a javascript that only applies to functionality on a particular page?

Should it be placed in the page's header code injection area OR should it be placed in the site wide footer code injection area?

This is one of those questions that cannot be definitely answered without knowing more about the code, what it does, how long it is, whether it relies on libraries and so on.

However, to generalise:

  • Place it in the footer
  • Keep the code in one place (for easy maintenance and troubleshooting)
  • Don't rely on third party libraries unless absolutely essential
  • Write the code well so that it only runs when the right conditions are met (for example, check for the required page content)
  • Optimise the code (keep it short)

That said, if you only require some code on one page (and no other page will require code) it is probably better to add it to a Code Block on the specific page.

Did this help? Please give feedback by clicking an icon below  ⬇️

About: Squarespace Circle Leader since 2017. I value honesty, transparency, diversity and great design ♥.
Work: Squarespace Developer and founder of SF Digital, building the features Squarespace didn't include™. 
Content: Links in my posts may refer to SF Digital products or may be affiliate links.

Catch up on all the release notes and announcements 2023 [for Circle members only]. There's a public version here too!
If I helped, you can thank me by clicking one of the emojis below. If you prefer, you can buy me a coffee.
Improve your online store with our extensions.

Link to comment
27 minutes ago, paul2009 said:

This is one of those questions that cannot be definitely answered without knowing more about the code, what it does, how long it is, whether it relies on libraries and so on.

However, to generalise:

  • Place it in the footer
  • Keep the code in one place (for easy maintenance and troubleshooting)
  • Don't rely on third party libraries unless absolutely essential
  • Write the code well so that it only runs when the right conditions are met (for example, check for the required page content)
  • Optimise the code (keep it short)

That said, if you only require some code on one page (and no other page will require code) it is probably better to add it to a Code Block on the specific page.

Did this help? Please give feedback by clicking an icon below  ⬇️

Thank you Paul for explaining that so well, much appreciated 🙏🏼

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.