Last active
December 20, 2015 13:18
-
-
Save valdiney/6137231 to your computer and use it in GitHub Desktop.
'Estudando Ajax': Mini biblioteca para uso de ajax! Tendo como referencias videos da 'Universidade XTI' e o livro 'Guia de um programador' do grande 'Maujor'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////// | |
// Estudando Ajax. | |
// Estudante: Valdiney França | |
//////////////////////////////////////////////////////// | |
window.onload = function() { | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// Exemplo de como requisitar os dados provindos de uma página php após o click de um botão | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
var chamar_ajax = document.getElementById('chamar_ajax').onclick = function() { | |
requisitar("dados.php"); | |
return false; | |
} // end chama_ajax | |
//////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////// | |
// Função carrega a imagem loader... | |
///////////////////////////////////////////////////////// | |
function carregando(c) { | |
while(c.hasChildNodes()) { | |
c.removeChild(c.lastChild); | |
} | |
var img = document.createElement('img'); | |
img.setAttribute('src', 'ajax-loader.gif'); | |
c.appendChild(img); | |
} | |
////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////// | |
// Função mostra na tela a requisição... | |
//////////////////////////////////////////////////// | |
function mostrar(ajax) { | |
var resposta = document.getElementById('resposta'); | |
while(resposta.hasChildNodes()) { | |
resposta.removeChild(resposta.lastChild); | |
} | |
resposta.appendChild(document.createTextNode(ajax.responseText)); | |
} | |
//////////////////////////////////////////////// | |
/////////////////////////////////////////////// | |
// Função faz a requisição dos dados... | |
/////////////////////////////////////////////// | |
function requisitar(url) { | |
var ajax = iniciaAjax(); | |
carregando(document.getElementById('resposta')); | |
ajax.onreadystatechange = function() { | |
if(ajax.readyState == 4) { | |
mostrar(ajax); | |
} | |
} | |
ajax.open("POST", url); | |
ajax.send(null); | |
} // end requisitar url | |
//////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////// | |
// Função verifica qual tipo de objeto o navegador está utilizando... | |
////////////////////////////////////////////////////////////////////// | |
function iniciaAjax() { | |
var ajax = null; | |
if(window.XMLHttpRequest) { | |
ajax = new XMLHttpRequest(); | |
} | |
else if(window.ActiveXObject) { | |
try { | |
ajax = new ActiveXObject("Msxml2.XMLHTTP"); | |
} catch(e) { | |
ajax = new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
} | |
return ajax; | |
} // end iniciar ajax... | |
////////////////////////////////////////////////////////////////// | |
} // end window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment