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
  • Views 227
  • 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 me: I've been a SQSP User for 18 yrs. I was invited to join the Circle when it launched in 2016. I have been a Circle Leader since 2017. I don't work for Squarespace. I value honesty, transparency, diversity and good design ♥.
Work: I founded and run SF.DIGITAL, building Squarespace Extensions to supercharge your commerce website. 
Content: Views and opinions are my own. Links in my posts may refer to SF.DIGITAL products or may be affiliate links.
Forum advice is free. You can thank me by clicking one of the feedback emojis below. Coffee is optional.

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.