  ws.onclose = function(evt) {
            msgs.innerHTML += '<li>WebSocket connection closed.</li>';
        }
        form.onsubmit = function() {
            ws.send(input.value);
            input.focus();
            return false;
        };
	
	window.onbeforeunload = function() {
	    ws.onclose = function () {}; // disable onclose handler first
	    if(ws.readyState == WebSocket.OPEN)
		ws.close();
            console.log('ws closed')
	};
	
	window.addEventListener("unload", function () {
	    if(ws.