2

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

jean-max
  • 1,640
  • 1
  • 18
  • 33
  • You can use this link to solve your problem. Comprehensive explanation about comparison of string. https://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string-in-javascript – Sohaib Feb 05 '22 at 11:44

1 Answers1

0

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

let pathname = window.location.pathname

if(pathname.includes('home'))
{
      do that
}
Evren
  • 4,147
  • 1
  • 9
  • 16
  • my var url contains several url's so i need that everytime it checks new url it should go to js. what your let pathname = window.location.pathname is doing? it does not pass a href="@url" – Ruslan Developer Feb 05 '22 at 11:48