Thursday, 9 April 2015

Play/ Pause Audio



Play/ Pause Audio
JavaScript:
document.getElementById("FullAudio").play();
document.getElementById("FullAudio").pause();

jQuery:
$("#FullAudio").get(0).play();
$("#FullAudio").get(0).pause();

Friday, 3 April 2015

Randomize the array items

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

Used like so
var arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);

XML Parser JavaScript and jQuery Ajax

XML File sample.xml

 <ACTIVITY>
    <title>Hellow</title>
</ACTIVITY>

JavaScript: 

if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
else // code for IE5 and IE6
        {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
xhttp.open("GET", "sample.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
alert(xmlDoc.getElementsByTagName("title")[0].innerHTML);


jQuery Ajax: 


$.ajax({
            type: "GET",
            url: "sample.xml",
            dataType: "xml",
            success: function(xml) {         
                var xmlDoc = $.parseXML(xml.activeElement.outerHTML),
                    $xml = $(xmlDoc);
                    alert($xml.find("title").text());
            },
            error: function() {
                alert(" Please refresh: Problem in loading xml.");
            }
        });