On a SharePoint online modern site, I need a JS variable to search for a specific string of text in the code of the page (this variable is for Google Tag Manager). I have tried:

function() {
    var pageContent = document.documentElement.innerHTML;
    if (pageContent.indexOf("TEXTTOSEARCHFOR") > -1) {
    return true; 
  } else {
    return false; 

It does not work, I get no result even when I can see the specific word in the code. I have tried outerHTML instead of innerHTML too.

Maybe this is because the HTML of a SPO page is handled in a different way than a more traditional web page?

This function was given to me by online friends, I know almost nothing in programming (but I know how Tag Manager quite well).

  • Are you using modern experience or classic experience in SharePoint online? Can you show us the screenshot of what exactly you are trying to search in web page? Mar 29, 2023 at 13:32
  • Also, check this: stackoverflow.com/questions/16550069/…. Let me know if it works for you. Mar 29, 2023 at 13:34
  • It's modern experience. I've tried various word, even Title didn't show any result (see example of page code below). I'm not sure how to use the example at the other page, sorry...(ex of page code: <html lang="fr-FR" dir="ltr"><head> <meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="GENERATOR" content="Microsoft SharePoint (Service worker)"> <link rel="shortcut icon" href="/_layouts/15/images/favicon.ico?rev=43" type="image/vnd.microsoft.icon" id="favicon"> <title>
    – dominique
    Mar 29, 2023 at 17:54
  • I am checking to see how things are going there on this issue. Apr 6, 2023 at 6:01

1 Answer 1


You can configure a search web part by following document


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.