I have a simple form inspired by this method for encoding svg files as data uri's.
The form submit is handled by vanilla Javascript and works fine, except that the page get's refreshed on the submit and I have to back to get the result. How can I stop the page refresh?
<body>
<form id="svgUriEncode">
<textarea rows="4" cols="50" id="svgIn"></textarea>
<button id="encode">encode</button>
<textarea rows="4" cols="50" id="svgOut"></textarea>
</form>
<script type="text/javascript">
var form = document.getElementById("svgUriEncode");
function encodeOptimizedSVGDataUri(form) {
var uriPayload = encodeURIComponent(form.svgIn.value) // encode URL-unsafe characters
.replace(/%0A/g, '') // remove newlines
.replace(/%20/g, ' ') // put spaces back in
.replace(/%3D/g, '=') // ditto equals signs
.replace(/%3A/g, ':') // ditto colons
.replace(/%2F/g, '/') // ditto slashes
.replace(/%22/g, "'"); // replace quotes with apostrophes (may break certain SVGs)
form.svgOut.value = 'data:image/svg+xml,' + uriPayload;
}
form.encode.addEventListener("click", function () {
encodeOptimizedSVGDataUri(form);
});
</script>