Home > database >  Is there a way to add a Script to an HTML file that will only affect the homepage of the site?
Is there a way to add a Script to an HTML file that will only affect the homepage of the site?

Time:02-04

I have a situation where I can only edit this overarching html file that affects all other pages on the site. It's not an ideal situation and this isn't what I usually do, so it's a bit difficult to navigate.

What I'd like to do is add a few <script> tags within the <head> tag and have the scripts only affect the .com homepage. Is it possible to accomplish that with just the use of the <script> tags and maybe some extra html?

CodePudding user response:

You could try something like the following perhaps...

Add this script to your index page.

<script>
    if(location.pathname == '/'){
        window.addEventListener('DOMContentLoaded', (event) => {
            var yourScript = document.createElement('script');
            yourScript.src = '[LOCATION TO YOUR SCRIPT HERE]';    
            document.head.appendChild(yourScript);
        });
    }
</script>
  •  Tags:  
  • Related