How can we help you today?

How to Make Custom JavaScript Execute on Partial Page Load on Modern Sites

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: 

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

World's best intranet sites are designed using ShortPoint

Get started today! Learn more
See all 15 topics

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more