Overview
The FreshJuice theme includes several JavaScript utility modules that provide common functionality for client-side operations. These modules are located insource/js/modules/ and are available throughout the theme.
Available Modules
loadScript
Dynamically loads external JavaScript files into the document. Prevents duplicate script loading and supports both async and defer loading strategies. Location:source/js/modules/_loadScript.js
Parameters
src(string, required): URL of the script to loadloading(string, optional): Loading strategy -"defer"(default) or"async"callback(function, optional): Function to execute when script finishes loadingcallbackForced(boolean, optional): Force callback execution even if script already exists (default:false)
Usage
Features
- Automatic protocol handling: Converts protocol-relative and relative URLs to HTTPS
- Duplicate prevention: Checks if script is already loaded before adding
- Cross-origin support: Adds
crossOrigin="anonymous"attribute - Callback support: Execute functions after script loads
- Force callback option: Run callback even if script was previously loaded
Example: Loading Google Maps
loadStylesheet
Dynamically loads external CSS stylesheets into the document. Prevents duplicate stylesheet loading and supports media queries. Location:source/js/modules/_loadStylesheet.js
Parameters
src(string, required): URL of the stylesheet to loadmedia(string, optional): Media query for the stylesheettype(string, optional): Type attribute (default:"text/css")
Usage
Features
- Automatic protocol handling: Converts protocol-relative and relative URLs to HTTPS
- Duplicate prevention: Checks if stylesheet is already loaded
- Media query support: Load stylesheets conditionally based on media queries
- Appends to head: Stylesheets are added to
<head>for proper cascade
Example: Loading Google Fonts
debugLog
Conditional logging utility that only outputs to console in development environments or when debugging is explicitly enabled. Location:source/js/modules/_debugLog.js
Parameters
...args(any): Arguments to log to the console
Usage
When Logs Appear
Logs are displayed in the console when either:- The hostname contains
.hs-sitesor.hubspot(HubSpot preview/staging environments) sessionStorage.juiceis set (manual debugging mode)
Features
- Environment-aware: Only logs in development/staging environments
- Manual override: Enable logging by setting
sessionStorage.juice = truein console - Visual identifier: All logs are prefixed with 🍹 emoji for easy identification
- Multiple arguments: Supports logging multiple values at once
Enable Manual Debugging
To enable debug logging in production, run this in the browser console:getSearchParams
Extracts and parses URL query parameters. Can retrieve a specific parameter or all parameters as an object. Location:source/js/modules/_getSearchParams.js
Parameters
param(string, optional): Specific parameter to retrieve (default:false- returns all)url(string, optional): URL to parse (default:false- uses current window location)
Returns
- If
paramspecified: Returns parameter value (string) ornullif not found - If
paramnot specified: Returns object with all parameters
Usage
Features
- Flexible parsing: Get all parameters or a specific one
- Custom URL support: Parse any URL, not just current page
- Hash handling: Automatically removes hash fragments before parsing
- Empty value handling: Parameters without values default to empty string
- Debug logging: Logs operations when debug mode is enabled
Example: UTM Parameter Tracking
Example: Form Pre-filling
Common Patterns
Lazy Loading Third-Party Scripts
Load heavy third-party scripts only when needed:Conditional Feature Loading
Load features based on URL parameters:Dynamic Theme Loading
Load different stylesheets based on user preferences:Best Practices
1. Use Debug Logging During Development
2. Prevent Duplicate Script Loads
loadScript and loadStylesheet automatically prevent duplicates, but you can also check manually: