A client has changed their CSP to ban inline styles on their server. As far as I can tell, this means that we can no longer use JS to dynamically position/animate/style HTML
The proper workaround for this issue is to use the CSS Object Model (CSSOM).
Given the following ways of setting the style:
<p style="left: 343px">...</p> // fails due to CSP
document.getElementById(id).setAttribute('style', 'left: 343px'); // fails due to CSP
document.getElementById(id).style.left = '343px';
Only the last one will successfully comply with a CSP directive of style-src: self
(because it's using the CSSOM).
That's why using jQuery's .css() function works:
When using .
css()
as a setter, jQuery modifies the element'sstyle
property. For example,$( "#mydiv" ).css( "color", "green" )
is equivalent todocument.getElementById( "mydiv" ).style.color = "green"
.
JavaScript is executed on the client. Unless the filtering software is incredibly clever, you can still add dynamic inline styles as the server has no idea what's happening in the browser. What you can't do, however, is add inline styles as part of the HTML you send to the client.