- Ajax (w3schools)
var req = new XMLHttpRequest();
// the request must be in the same domain as the containing webpage
req.open("GET", "/cgi-bin/test.cgi", false);
req.send();
document.write(req.responseText);
- download file (workaround for "save to a local file")
var s = "text";
var blob = new Blob([s]);
var o = URL.createObjectURL(blob);
var e = document.createElement("a");
e.href = o;
e.innerHTML = "link";
e.download = "save_as_filename.txt";
document.body.appendChild(e);
// creates a new link at the bottom of the page, that can be saved locally
- enumerate NodeList
var aElem = document.getElementsByTagName("input");
Array.prototype.forEach.call(aElem, function(e) {
console.log(e.name);
});
- events
var inp = document.body.getElementsByTagName("input").item(3);
inp.onkeydown = null
inp.onkeypress = null;
document.onkeypress = function (e) {
console.log(String.fromCharCode(e.charCode) + '=' + e.charCode);
// getting int from str, i.g. parseInt("a0", 16)
// to get char code: str.charCodeAt()
return false;
};
elem.addEventListener("click", function(){
alert("event");
});
aa
- logging
console.log('something');
- modifying document
span = document.getElementById('dataMod');
if (span != null) {
data = dataMod.textContent.substring(8, 18);
span.textContent = data.substring(8, 10) + '.' + data.substring(5, 7)
+ '.' + data.substring(0, 4);
}
- style, toggleVisibility
<button onclick='toggleVisibility()'>Rozwiązanie</button>
<div id="sol">
<script>
var bVisible = true;
function toggleVisibility()
{
bVisible = !bVisible;
sVisible = bVisible ? "initial" : "hidden"; // "visible"
document.getElementById('sol').style.visibility = sVisible;
}
toggleVisibility();
</script>
b</div>
- timeout
window.setTimeout(fun, 1000); /* possible args after timeout in ms */
// cancel all timeouts http://stackoverflow.com/a/8860203/772981
var id = window.setTimeout(function() {}, 0);
while (id--) {
window.clearTimeout(id); // will do nothing if no timeout with id is present
}