Home > Mobile >  How run a script only on the homepage?
How run a script only on the homepage?

Time:10-22

There is a way to run the follow script only on the homepage?

<script>

   let changelink = document.querySelector('#app > div.boxes.cf > a');

   changelink.outerHTML = '<a href="#contact">contact us</a>';

</script>

I have tried this but it is not working. The script is still running on all the pages

<script>

   if (window.location.href.indexOf('/') != -1) {
    
      let changelink = document.querySelector('#app > div.boxes.cf > a');
    
      changelink.outerHTML = '<a href="#contact">contact us</a>';
   }

</script>

CodePudding user response:

try:

if (window.location.href.replaceAll(window.location.origin, '').replaceAll('/','').length === 0 ) {

  let changelink = document.querySelector('#app > div.boxes.cf > a');

  changelink.outerHTML = '<a href="#contact">contact us</a>';

} 
  • Related