document.title value for modern SharePoint pages

Christophe Vandeput 96 Reputation points
2021-03-02T09:50:18.357+00:00

Hi,

I have some JavaScript injected on my modern SPPages.
JavaScript file hosted in the MasterPageGallery and injected by Application Customizer (SPFx).

In the JavaScript code I want to use document.title to log some data to an external system at page loading.
Now I see that my external system (very) often receives the title of previous page. So it seems that document.title is not updated yet.
Is this due to partial loading of page components of modern pages?

Any idea's?

Kind Regards

Vandeput

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,565 questions
0 comments No comments
{count} votes

Accepted answer
  1. Christophe Vandeput 96 Reputation points
    2021-03-11T10:24:33.613+00:00

    Hi, I did some testing.
    And my issue is due to the partial page loading mechanism of Modern SharePoint pages.
    In some cases the document.title isn't updated correctly when I need it, it still the old value. If you wait few moment, it will be updated with the new version.

    So I need to do this in my Application Customizer itself.
    Check to following post for more info : https://blog.velingeorgiev.com/page-hit-when-SPA-page-transitioning-modern-sharepoint-sites


2 additional answers

Sort by: Most helpful
  1. Jerryzy 10,561 Reputation points
    2021-03-03T07:32:04.73+00:00

    Hi @Christophe Vandeput ,

    document.title should be updated when the modern page title changed, I tested with application customizer inject with a simplest JavaScript file like this, just like this:

    73685-snipaste-2021-03-03-15-27-54.png

    Then inject in Application Customizer:

    73580-snipaste-2021-03-03-15-28-58.png

    After deploying to App Catalog, it's working as expected for the document.title value update:

    Previous:

    73711-snipaste-2021-03-03-15-30-22.png

    Updated:

    73650-snipaste-2021-03-03-15-31-03.png

    Thanks
    Best Regards


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    0 comments No comments

  2. Christophe Vandeput 96 Reputation points
    2021-03-03T16:06:59.183+00:00

    I will try to simulate like you did with an alert. I will update when I have more info.