JavaScript Snippets

This page contains some code snippets in JavaScript by Ron Spain. You should expect this page to grow over the next month after my power supply arrives. You can consider these snippets public domain.

// shorthand for document.getElementById function get(s){return typeof s!="string"?s:document.getElementById(s)} // puts string b into element a function put(a,b){var e=get(a);if("innerHTML" in e)e.innerHTML=b;else e.value=b} // returns non-negative integer less than n function rnd(n){return Math.floor(Math.random()*n)} // replaceAll method for strings String.prototype.replaceAll=function(a,b){return this.split(a).join(b)}; // moves element to coordinates if position is absolute function mv(e,x,y){ e=get(e).style; e.left=x+"px";"px"; }

©2019 Ron Spain