Eliminating Passive Listeners JS Syntax Issue

276 Views Asked by At

I'm trying to eliminate Passive Listeners to improve scrolling performance. But every time I add the code, my syntax breaks my home page slider. Here is my current integration syntax:

<script>
function clean(node)
{
  for(var n = 0; n < node.childNodes.length; n ++)
  {
    var child = node.childNodes[n];
    if
    (
       child.nodeType === 8 
       || 
       (child.nodeType === 3 && !/\S/.test(child.nodeValue))
     )
     {
       node.removeChild(child);
       n --;
     }
     else if(child.nodeType === 1)
     {
       clean(child);
     }
   }
 }
 document.addEventListener("DOMContentLoaded", doClean);

 function doClean(){
     clean(document.body);
 }
 </script>

I've tried adding:

document.addEventListener("touchstart", function(e) {
    console.log(e.defaultPrevented);  // will be false
    e.preventDefault();   // does nothing since the listener is passive
    console.log(e.defaultPrevented);  // still false
}, Modernizr.passiveeventlisteners ? {passive: true} : false);

Which boosts my Page Score dramatically. But like I said "It completely removes my slider from the home page". I think I'm just not writing the JS syntax correctly. It wouldn't make sense to add 2 "document.addEventListener" would it? Any suggestions?

My website: https://www.staging3.easyimportauto.com/

Thanks

Here's where I'm at now (I've got 2 EventListeners):

document.addEventListener("DOMContentLoaded", doClean);

   function doClean(){
       clean(document.body);
}
document.addEventListener("touchstart", function(e) {
   console.log(e.defaultPrevented);  // will be false
   e.preventDefault();   // does nothing since the listener is passive
   console.log(e.defaultPrevented);  // still false
}, Modernizr.passiveeventlisteners ? {passive: false});

The above JS syntax does indeed eliminate the Error in Google PageSpeed Insights...but it still is making my Revolution Slider/Banner disappear on the home page. There must still be an error in my above code? Or do you think this is beyond the scope of the Passive Listener addition? hmmm lol

My website: https://www.staging3.easyimportauto.com/

0

There are 0 best solutions below