Iam sharing my custom js code that iam injecting in my squarespace website but facing an error in report of google page speed insights (error exists only when i add the custom code, otherwise no error).
[This is screenshot of report where the error is comming]
(https://i.sstatic.net/jyZYNRWF.png)
<script>
if (!sessionStorage.getItem('initialScriptExecuted')) {
var preloader = document.createElement("div");
preloader.textContent = "Please wait....";
preloader.id = "preloader";
preloader.style.cssText = "position:fixed;top:0;left:0;width:100%;height:100%;background-color:#fff;z-index:10000;display:flex;font-size:14px;justify-content:center;align-items:center";
document.documentElement.appendChild(preloader);
sessionStorage.setItem('initialScriptExecuted', 'true');
setTimeout(function() {
location.reload(true);
}, 4000);
} else {
var preloader = document.getElementById('preloader');
if (preloader) {
preloader.remove();
}
}
</script>
I have tried to add the charset tag dynamicall using this script before and after the main script causing the error but still error does not resolve.
<script>
// Check if the charset meta tag is already present
var charsetMeta = document.querySelector('meta[charset]');
// If not present, dynamically create and insert it as the first element in the head
if (!charsetMeta) {
var newCharsetMeta = document.createElement('meta');
newCharsetMeta.setAttribute('charset', 'UTF-8');
document.head.insertBefore(newCharsetMeta, document.head.firstChild);
}
</script>
Sajid Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.