Created
December 19, 2012 21:22
-
-
Save oliveiraev/4340621 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE HTML> | |
<html lang="pt-BR"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<form action="" method="get"> | |
<fieldset> | |
<legend>Busca de CEP do Correio Control</legend> | |
<ul> | |
<li> | |
<label for="cep"> | |
<span>CEP:</span> | |
<input name="cep" id="cep" type="text" maxlength="8" size="8" autofocus="autofocus" autocomplete="autocomplete" x-webkit-speech="x-webkit-speech" /> | |
</label> | |
</li> | |
<li> | |
<label for="logradouro"> | |
<span>Logradouro:</span> | |
<input name="logradouro" id="logradouro" type="text" size="30" /> | |
</label> | |
</li> | |
<li> | |
<label for="bairro"> | |
<span>Bairro:</span> | |
<input name="bairro" id="bairro" type="text" size="15" /> | |
</label> | |
</li> | |
<li> | |
<label for="cidade"> | |
<span>Cidade:</span> | |
<input name="cidade" id="cidade" type="text" size="20" /> | |
</label> | |
</li> | |
<li> | |
<label for="uf"> | |
<span>UF:</span> | |
<input name="uf" id="uf" type="text" maxlength="2" size="2" /> | |
</label> | |
</li> | |
</ul> | |
</fieldset> | |
</form> | |
<p><a href="http://avisobrasil.com.br/api-de-consulta-de-cep/">Documentação oficial</a></p> | |
<script type="text/javascript"> | |
(function (w) { | |
"use strict"; | |
var d, uf, cep, url, head, bairro, cidade, logradouro, script; | |
d = w.document; | |
url = 'http://cep.correiocontrol.com.br/%s.js'; | |
function eventListener(element, event, listener) { | |
if (element.addEventListener) { | |
element.addEventListener(event, listener); | |
return; | |
} | |
event = 'on' + event; | |
if (element.attachEvent) { | |
element.attachEvent(event, listener); | |
return; | |
} | |
element[event] = listener; | |
} | |
function preencheCep() { | |
var valor; | |
if (!preencheCep.lastVal) { | |
preencheCep.lastVal = 1; | |
} | |
valor = cep.value.replace(/\D/g, ''); | |
if (valor.length < 8) { | |
return; | |
} | |
if (cep.value === preencheCep.lastVal) { | |
return; | |
} | |
if (script) { | |
head.removeChild(script); | |
} | |
preencheCep.lastVal = cep.value; | |
script = d.createElement('script'); | |
script.id = 'atualizaCep'; | |
script.src = url.replace('%s', valor); | |
script.type = 'text/javascript'; | |
head.appendChild(script); | |
} | |
function atualizaCep(valor) { | |
if (valor.erro) { | |
w.alert('Cep não encontrado.'); | |
return; | |
} | |
uf.value = valor.uf; | |
bairro.value = valor.bairro; | |
cidade.value = valor.localidade; | |
logradouro.value = valor.logradouro; | |
} | |
function encontraElementos() { | |
if (encontraElementos.pular) { | |
return; | |
} | |
uf = d.getElementById('uf'); | |
cep = d.getElementById('cep'); | |
head = d.getElementsByTagName('head')[0]; | |
bairro = d.getElementById('bairro'); | |
cidade = d.getElementById('cidade'); | |
logradouro = d.getElementById('logradouro'); | |
eventListener(cep, 'keyup', preencheCep); | |
encontraElementos.pular = true; | |
} | |
function encontraElementosIE() { | |
var states = ['interactive', 'complete', 'loaded']; | |
while (states.length) { | |
if (d.readyState === states.shift()) { | |
encontraElementos(); | |
return; | |
} | |
} | |
} | |
eventListener(d, 'DOMContentLoaded', encontraElementos); | |
eventListener(d, 'readystatechange', encontraElementosIE); | |
eventListener(w, 'load', encontraElementos); | |
w.correiocontrolcep = atualizaCep; | |
}(window)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment