var serializer = new XMLSerializer(); var script = document.createElement('script'); script.textContent = 'var n = 1'; var str = serializer.serializeToString(script); alert(str);