Tuesday, March 19, 2019

TypeError: Assignment to read-only properties is not allowed in strict mode

Get around this pitfall in Internet Explorer by changing assignments to style to instead be assignments to style.cssText! It does not break Google Chrome! I started with this:

var loop = function(){
   let offsetHeight:number = document.getElementById('content').offsetHeight;
   let heightStyle: string = "height: " + offsetHeight + "px;";
   var widemenu:any = document.getElementsByName('widemenu');
   if (widemenu && widemenu[0]) {
      widemenu[0].style = heightStyle;
   }
   var narrowmenu:any = document.getElementsByName('narrowmenu');
   if (narrowmenu && narrowmenu[0]) {
      narrowmenu[0].style = heightStyle;
   }
};
var interval = setInterval(loop, 500);

 
 

I revamped like so:

var loop = function(){
   let offsetHeight:number = document.getElementById('content').offsetHeight;
   let heightStyle: string = "height: " + offsetHeight + "px;";
   var widemenu:any = document.getElementsByName('widemenu');
   if (widemenu && widemenu[0]) {
      widemenu[0].style.cssText = heightStyle;
   }
   var narrowmenu:any = document.getElementsByName('narrowmenu');
   if (narrowmenu && narrowmenu[0]) {
      narrowmenu[0].style.cssText = heightStyle;
   }
};
var interval = setInterval(loop, 500);

No comments:

Post a Comment