I was trying to add an accessibility feature to my web page, which is built with React.js, to make sure that the color of my banner doesn't get affected by the high contrast plugin of Chrome.
I tried to implement it that by adding a function to detect whether the Chrome plugin has been turned on, and my code should toggle a class to make sure the banner color won't change much in the high contrast mode. I noticed that this change could only be seen after refreshing the page, so I added this line of code window.location.reload() to manually reload the page.
The problem is, this part of code enters into an infinite loop and my page just won't stop reloading. I tried to replace the reloading part with other methods, the results were still the same. Here's the code of my React Component:
componentDidMount = () => {
this.keepBannerColor()
}
keepBannerColor() {
// these two lines of code below is tell whether the browser is chrome
let userAgentString = navigator.userAgent;
let chromeAgent = userAgentString.indexOf("Chrome") > -1;
let header = document.querySelector('.mizaru-header')
if (header && chromeAgent) {
console.log('funciton triggered')
let htmlTag = document.getElementsByTagName('html');
let isInverted = htmlTag[0].getAttribute('hc') != null
header.classList.toggle('inverted', isInverted)
window.location.reload()
}
}
Here's my CSS code for the toggle class: .inverted{ filter: invert(100%); }
Any help would be nice!