…
…
…
…
Nel tutorial di oggi vedremo come creare una utile funzione Javascript che permette di convertire XML in JSON.
// Changes XML to JSON function xmlToJson(xml) { // Create the return object var obj = {}; if (xml.nodeType == 1) { // element // do attributes if (xml.attributes.length > 0) { obj["@attributes"] = {}; for (var j = 0; j < xml.attributes.length; j++) { var attribute = xml.attributes.item(j); obj["@attributes"][attribute.nodeName]…
In questo semplice tutorial javascript venderemo come cercare un numero all’interno di un array.
(function () { // must be sorted array. var aList = [2, 6, 9, 32, 33, 45, 47, 65]; nElemToFind = 6, nMinRange = 0, nMaxRange = aList.length-1, bResult = false; while (nMinRange < = nMaxRange) { var nMid = Math.floor((nMinRange + nMaxRange) / 2); if (nElemToFind === aList[nMid]) { bResult = true; console.log(bResult)…
Nel tutorial di oggi vedremo come caricare in runtime files js e CSS.
Creeremo una funzione che prende in ingresso due parametri, il primo definisce il nome del file, il secondo invece il tipo di file che si intende caricare. Iniziamo!
function loadjscssfile(filename, filetype) { if(filetype=="js"){ //if filename is a external JavaScript file var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename) } else if(filetype=="css"){ //if filename is an external CSS file var fileref=document.createElement("link") fileref.setAttribute("rel", "stylesheet")…
Nel tutorial di oggi imparerete come creare una funzione che converte un colore RGB in esadecimale utilizzando jQuery.
function rgb2hex(rgb) { //convert rgb value to hex rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/); function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2); } return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); …