commonNav.js 1.0 KB

12345678910111213141516171819202122232425262728
  1. if (typeof fetch === 'function') {
  2. const init = () => {
  3. if (typeof scrollToNavItem !== 'function') return false
  4. scrollToNavItem()
  5. // hideAllButCurrent not always loaded
  6. if (typeof hideAllButCurrent === 'function') hideAllButCurrent()
  7. return true
  8. }
  9. fetch('./nav.inc.html')
  10. .then(response => response.ok ? response.text() : `${response.url} => ${response.status} ${response.statusText}`)
  11. .then(body => {
  12. document.querySelector('nav').innerHTML += body
  13. // nav.js should be quicker to load than nav.inc.html, a fallback just in case
  14. return init()
  15. })
  16. .then(done => {
  17. if (done) return
  18. let i = 0
  19. ;(function waitUntilNavJs () {
  20. if (init()) return
  21. if (i++ < 100) return setTimeout(waitUntilNavJs, 300)
  22. console.error(Error('nav.js not loaded after 30s waiting for it'))
  23. })()
  24. })
  25. .catch(error => console.error(error))
  26. } else {
  27. console.error(Error('Browser too old to display commonNav (remove commonNav docdash option)'))
  28. }