Introduction
By default, when adding Custom JavaScript in the Theme Builder, it will run on all site pages where ShortPoint is running, but the code will only be executed when the page is initially loaded, or when a full page reload is done. Modern SharePoint pages use a custom router and are only partially loaded when navigating from one to another, which prevents the ShortPoint Custom JS code from being executed again. Here's what you can do.
Before we start:
- This article is for the Modern SharePoint Experience.
- You need to have ShortPoint SPFx installed in your environment.
- You need to have some basic JavaScript knowledge.
Step 1.
Launch the ShortPoint Theme Builder by navigating to Site Contents > ShortPoint Dashboard > Theme Builder.
Step 2.
Insert the following code:
shortpoint.subscribe(shortpoint.settings.events.pushStateChanged, function(state) { if(state == null || !state.url) { return; } // this code will only run when the page is partially loaded alert('code running when page is partially loaded'); }); // this code will only run when the page is fully reloaded alert('code running on full page load');
It should look like this:
Modify the code according to your needs. If you want to have some code running on both full page reload and on partial page reloads when the SharePoint router intercepts requests, it will have to be added twice in the areas marked by comments in the snippet above.
Finally click Apply and then Publish in order to for these changes to take effect. Your code should be now executed according to the script you have applied.
Related Articles: