I'm having issues setting up a proper way of escaping strings and sanitizing data sent from the server to the client.
I've tried the encodeURI function built within Javascript, however the names in my chat look very chunky having %'s in them etc.
function sendChat(data){
var message = `<div class="message flex">
<div class="image flex flex-v flex-h">
<img src="${ data.avatar }">
</div>
<div class="content">
<div class="name flex">
<div>
${ data.username }
</div>
<div class="level">
${ data.level }
</div>
</div>
<div class="text">
${ data.message }
</div>
</div>
</div>`;
$('.chat .box').append(message);
}
Whenever a user puts in alert('hi') as an text message it is obviously sent to the the client and converted into proper js, however to prevent this I'd like to sanitize the data without it looking funky, is there any proper function to use so I can still safely sanitize the data without it looking scuffed..?