logo
search
Others

How to Run a Custom Script Before Other Scripts on SharePoint Online

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

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 you start

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.

Solution 1Recommended

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.

1
Evaluate SPFx Application Customizers

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.

2
Post in Microsoft Q&A

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.

3
Avoid DOM Dependency

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.

Important: Microsoft actively blocks traditional custom script features on modern sites by default. Always rely on SPFx rather than legacy Script Editor web parts.
Free Microsoft Office alternative

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. 1. Download the Installer: Visit the official WPS Office website and download the free installer for your operating system.
  2. 2. Install WPS Office: Run the setup file and follow the straightforward on-screen instructions to install the suite in minutes.
  3. 3. Open Your Office Files: Launch WPS Office to easily open, edit, and save your existing Microsoft Office documents without formatting loss.
Fully compatible with Microsoft Word, Excel, and PowerPoint file formats.Lightweight installation that consumes minimal system resources while you code.Includes a powerful built-in PDF toolkit for reading and editing technical specifications.Familiar tabbed interface allows you to manage multiple documents in a single window.
microsoft office alternative - wps office

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.