AkankshaK Posted January 14, 2023 Share Posted January 14, 2023 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
Solution paul2009 Posted January 14, 2023 Solution Share Posted January 14, 2023 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 ⬇️ Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥. Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links. Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional. Would you like your customers to be able to mark their favourite products in your Squarespace store? Link to comment
AkankshaK Posted January 14, 2023 Author Share Posted January 14, 2023 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment