How to detect any change in SharePoint Server 2019/Subscription Edition browser URL?

Raghunandan Kumbhar 0 Reputation points
2025-03-25T14:13:23.01+00:00

We have an SPFx Application Customizer, which checks the URL in the browser, and checks whether a file is open in preview mode. If the URL is of the preview mode, we want to redirect the user to another URL (our own custom editor). When I click on the filename, the file opens in the default SharePoint text editor, and the URL in the browser is that of preview mode, but my Application Customizer does not get triggered in this instance. But if I reload the page after the file is already open in the default text editor, my Application Customizer gets triggered, and I am able to redirect the user to another url.

The ideal behavior is that after the user clicks on the filename and the default text editor loads up, the user should get redirected to our custom url without needing any further action from the user.

Is there any way for us to get an event when the URL changes? Or is there any other implementation possible to achieve the same behavior?

Any guidance on how this can be implemented will be greatly appreciated.

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,655 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,446 Reputation points Microsoft External Staff
    2025-03-26T02:37:57.1866667+00:00

    Hi @Raghunandan Kumbhar

    To detect URL changes in SharePoint Server 2019/Subscription Edition using SPFx, you can monitor client-side URL modifications by combining the popstate event with monkey-patched history methods. Here is the code for reference

      private handleUrlChange = (): void => {
        const currentUrl = window.location.href;
        if (currentUrl !== this.previousUrl) {
          console.log(`URL changed from ${this.previousUrl} to ${currentUrl}`);
          // Add custom logic here (e.g., trigger updates, log changes)
          this.previousUrl = currentUrl;
        }
      }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.