Yes You can send JavaScript and CSS via WebSockets (or AJAX, for that matter). You also do not need base64 to encode CSS and JavaScript in the same way as an image if the WebSocket server correctly encodes UTF-8 any Unicode special characters in Javascript.
After you get Javascript or CSS via WebSocket, you can load them using the following mechanism (where type is "script" or "css"):
function dynamic_load(type, content) { var elem = document.createElement(type); elem.type = (type === 'script') ? 'text/javascript' : 'text/css'; elem.innerHTML = content; document.getElementsByTagName("head")[0].appendChild(elem); }
This mechanism may have problems in IE 8 and earlier, but since you are using WebSockets, I suspect that your goal is modern browsers. You can check if the dynamic_load function works from the Javascript browser console:
dynamic_load('script', "alert('hello world');");
source share