'javascript check if a string exists on page

I am working on checking if string exists on page. And I have some difficulties. I have url that generated by system var url = here it generated it in string format and its in "string" format. So lets say it has for of "url/my" and "url/yours" I make it to be url by <a href="@url" target="_blank">@text</a> I need to check if it contains exact word on page in generated url

<script type='text/javascript'>
  window.onload = function () {
    if (document.body.innerHTML.toString().indexOf('something') > -1) {
      alert("It contains 'something'");
    }
  };
</script>

This is working on the page that was opened, but not on the page of the generated URL. I need to make it work in IF-ELSE statement. Something like

if(url contain word "something")
{
  do that
}

So I need somehow pass generated url with href to script and then to if-else statement



Solution 1:[1]

You can check url pathname and then make control in your function

let pathname = window.location.pathname

if(pathname.includes('home'))
{
      do that
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evren