You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
OGordoo f6f4dbe63a removing npm, correcting subject 4 years ago
..
README.md removing npm, correcting subject 4 years ago

README.md

Routing example

The example consists in switching between a 'Home Page' to a 'Active Page' by clicking on a 'Click Me' hyperlink.

You will need to two or three files depending if you include the script on the html files or not. All files should be in the same folder.

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
  </head>
  <body>
    <h1>Home Page</h1>
    <a href="./active">Click here</a>
    <script src="./script.js"></script>
  </body>
</html>

active.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Active</title>
  </head>
  <body>
    <h1>Active Page</h1>
    <a href="/">Click Me</a>
    <script src="script.js" async defer></script>
  </body>
</html>

script.js:

const getTextFile = (file, allText = '') => {
  let rawFile = new XMLHttpRequest()
  rawFile.open('GET', file, false)
  rawFile.onreadystatechange = () => {
    if (rawFile.readyState === 4) {
      if (rawFile.status === 200 || rawFile.status == 0) {
        allText = rawFile.responseText
      }
    }
  }
  rawFile.send(null)
  return allText
}

document.body.innerHTML =
  location.pathname === '/'
    ? getTextFile('./index.html')
    : getTextFile('./active.html')