New Script Loading Strategy Deployed to Enhance Website Functionality
A refined approach to loading essential website scripts has been implemented, aiming to optimize performance and user experience. This strategy involves dynamically injecting scripts for Google Publisher Tag (GPT) and Facebook SDK, along with conditional loading of a light gallery plugin, triggered by user interaction.
The core of this update lies in a new initialization function, `initialize()`, designed to defer the loading of certain resources until the user actively engages with the page. This is achieved by attaching event listeners for `mousemove`, `scroll`, and `touchstart` events. once any of these events occur, the `initialize()` function is called, setting an `initiate` flag to zero to prevent subsequent executions.
Upon initialization, the Google Publisher tag script (`gpt.js`) is appended to the `
` section of the document, ensuring its availability for ad serving. Concurrently, the Facebook SDK script (`sdk.js`) is prepended to the ``,facilitating Facebook integration features.A key feature of this update is its conditional execution of a light gallery plugin. If an element with the ID `lightgallery` is detected on the page, the system proceeds to load the necessary CSS and JavaScript files for the light gallery. This includes a stylesheet from `https://www.okaz.com.sa/css/lightgallery.min.css` and the `lightgallery-all.min.js` script from `https://www.okaz.com.sa/js/lightgallery-all.min.js`. Following a two-second delay, a function named `lightGalleryLoad()` is invoked to initialize the light gallery with specific configurations, targeting elements with the class `itemLightGallery` for gallery items.
This dynamic loading mechanism is intended to improve initial page load times by not burdening the user with all scripts upfront. Instead,resources are loaded as they are likely to be needed,based on user interaction patterns.