How to Run a Custom Script Before Other Scripts on SharePoint Online
Question details
The user needs to execute a custom script on all classic and modern SharePoint Online pages before any other scripts load, without depending on standard SharePoint objects.
- Product
- SharePoint Online
- Device & OS
- not provided
- Scenario
- Deploying a custom script where existing SharePoint Framework (SPFx) extensions and web parts load too late in the page lifecycle.
- Observed behavior
- Current supported customization models like SPFx extensions load after the initial page scripts, preventing the custom script from executing first.
Before attempting to inject custom scripts into modern SharePoint, recognize that directly modifying the page execution order is strictly governed by Microsoft's supported development frameworks to ensure tenant security and stability.
Use SharePoint Framework (SPFx) and Consult Developer Communities
Since custom script injection before framework initialization is not natively supported in modern SharePoint, you should adapt your solution to SPFx or seek guidance from Microsoft developer channels.
Modern SharePoint Online heavily restricts custom script injection to ensure that automated updates from Microsoft do not break tenant customizations. Trying to forcefully run a script before standard SharePoint scripts often involves unsupported DOM manipulation.
SharePoint Framework (SPFx) is the only officially supported customization model for modern pages. If an SPFx Application Customizer loads too late for your specific need, you must consult Microsoft's developer community for alternative architectural approaches.
Review your script's requirements to determine if it can be refactored into a SharePoint Framework (SPFx) Application Customizer, which allows embedding JavaScript securely across all modern site pages.
Navigate to the Microsoft Q&A portal and post your specific execution order scenario using the 'SharePoint Development' tag to receive insights from Microsoft engineers.
Remove any dependencies on page DOM elements or CSS structures in your custom script, as these are dynamic in SharePoint Online and will eventually cause your script to fail when Microsoft updates the UI.
Looking for a Lightweight Office Suite for Your Development Work?
While you manage complex SharePoint Online development tasks, you need a fast and reliable office suite to write documentation, track project requirements, and manage code snippets. WPS Office offers a completely free, highly compatible alternative to Microsoft Office.
- 1. Download the Installer: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Install WPS Office: Run the setup file and follow the straightforward on-screen instructions to install the suite in minutes.
- 3. Open Your Office Files: Launch WPS Office to easily open, edit, and save your existing Microsoft Office documents without formatting loss.

Frequently Asked Questions
Can I use classic Script Editor web parts on modern SharePoint pages?
No, classic Script Editor web parts are not natively supported on modern SharePoint Online pages for security reasons. You must use the SharePoint Framework (SPFx) to deploy custom JavaScript securely.
Why does Microsoft discourage relying on SharePoint page DOM elements?
Microsoft continuously updates SharePoint Online's underlying HTML and CSS structure to introduce new features. Custom scripts that target specific DOM elements or CSS classes are highly likely to break during these routine updates.
How can I get support for advanced SharePoint customization?
You should post your specific requirements in the Microsoft Q&A forum using the 'SharePoint Development' tag, or participate in the SharePoint Developer community discussions to get help from experts.




