Run function only on index.html page (Javascript)

sthig

I have a function that runs a random word generator on my index.html page, it works just fine however it continues to run when you're on any other page as well (because of windows.onload).

I am unsure how to write javascript to say "only run this function on index.html, no other page" or "just on this class"

window.onload = function refreshWord()
  setInterval(function randomWord() {
    const words = ['Engineer', 'Developer', 'Illustrator', 'Cyclist', 'Artist'];
    const randomGenerator = Math.floor(Math.random() * words.length);
    document.getElementById('textswap').innerHTML = words[randomGenerator];
  }, 2000);
        <div class="col">
          <h4>These are the words, a <span class="front-page-text-swap" id="textswap"> person </span>.</h4>
        </div>

My end goal is to have the function refreshWord() execute only on the index.html page and no other however I'm at a loss at how to do that (and I'd like to use vanilla js, not jQuery)

CertainPerformance

Check to see if the pathname is index.html, and if it is, create the interval:

if (window.location.pathname === '/index.html') {
  setInterval( ...
}

It would also be nice to only select textswap once, rather than every time the interval runs, and to only create one words array, rather than declare a new array every time:

if (window.location.pathname === '/index.html') {
  const words = ['Engineer', 'Developer', 'Illustrator', 'Cyclist', 'Artist'];
  const getRandomWord = () => words[Math.floor(Math.random() * words.length)];
  const textswap = document.getElementById('textswap');
  setInterval(() => {
    textswap.textContent = getRandomWord();
  });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Only run a function if on X page

Run a hidden javascript function with in a page

How to run javascript/html page

Only run JavaScript function once

Run JavaScript only if a Div is found in the page

Refresh page and run function after JavaScript

Run JavaScript function on page load as well as on setInterval

The best way to run a function javascript in page load

How do I run this JavaScript function and HTML only if a specific cookie does not exist

javascript not loading on index.html page

How to run a javascript in a dynamically generated html page?

Javascript won't run on my HTML page

only run javascript function after certain date

Run function only once - functional programming JavaScript

run JavaScript function only if Boolean is true

Run JavaScript function only if user logged in - Django

addEventListener is running function on page load only javascript

To open a new page and run a javascript function in that new page

Run function only after paypal transaction success and redirected to my page

HTML on-click button to run javascript function

HTML won't run JavaScript function

Run a javascript function from HTML events

Run Function on default HTML Select with PHP And Javascript

Bulma Navbar burger only working on index.html page

Does Cloud Foundry only read/loads the index.html page?

Run Javascript function only if 'beforeunload' function returns false

Unable to view the results of a JavaScript function on a HTML page

Javascript to Modify a function in HTML and reload the page

Link to a HTML page inside a Javascript function