Carregamento simples de uma página dentro de uma DIV usando AJAX


Primeiro criamos o arquivo onde fazemos a solicitação do browser, para saber se o navegador suporta ou não “Msxml2.XMLHTTP”

ajax.js


1. function GetXMLHttp() {
2. if(navigator.appName == "Microsoft Internet Explorer") {
3. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
4. }
5. else {
6. xmlHttp = new XMLHttpRequest();
7. }
8. return xmlHttp;
9. }
10.
11. var xmlRequest = GetXMLHttp();

function GetXMLHttp() {
if(navigator.appName == "Microsoft Internet Explorer") {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}

var xmlRequest = GetXMLHttp();


No arquivo “instrucao.js” terá as informações necessárias para fazer a ação

instrucao.js

1. function abrirPag(valor){
2. var url = valor;
3.
4. xmlRequest.open("GET",url,true);
5. xmlRequest.onreadystatechange = mudancaEstado;
6. xmlRequest.send(null);
7.
8. if (xmlRequest.readyState == 1) {
9. document.getElementById("conteudo_mostrar").innerHTML = "<img src='loader.gif'>";
10. }
11.
12. return url;
13. }
14.
15. function mudancaEstado(){
16. if (xmlRequest.readyState == 4){
17. document.getElementById("conteudo_mostrar").innerHTML = xmlRequest.responseText;
18. }
19. }

function abrirPag(valor){
var url = valor;

xmlRequest.open("GET",url,true);
xmlRequest.onreadystatechange = mudancaEstado;
xmlRequest.send(null);

if (xmlRequest.readyState == 1) {
document.getElementById("conteudo_mostrar").innerHTML = "<img src='loader.gif'>";
}

return url;
}

function mudancaEstado(){
if (xmlRequest.readyState == 4){
document.getElementById("conteudo_mostrar").innerHTML = xmlRequest.responseText;
} }


Criamos a página Index.html para recerber as informações

Index.html

1. <html>
2. <head>
3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4. <title>Carregando Página em DIV / AJAX</title>
5. <script language="javascript" src="ajax.js"></script>
6. <script language="javascript" src="instrucao.js"></script>
7. </head>
8. <body>
9. <div id="menu"><a href="#" onclick="abrirPag('Conteudo.html');">Clientes</a></div>
10. <br><br>
11. <div id="conteudo_mostrar"></div>
12. </body>
13. </html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Carregando Página em DIV / AJAX</title>
<script language="javascript" src="ajax.js"></script>
<script language="javascript" src="instrucao.js"></script>
</head>
<body>
<div id="menu"><a href="#" onclick="abrirPag('Conteudo.html');">Clientes</a></div>
<br><br>
<div id="conteudo_mostrar"></div>
</body>
</html>


E finalmente criamos a página Conteudo.html que será exibida dentro da DIV “conteudo_mostrar”

 

Conteudo.html

1. <html>
2. <head>
3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4. <title>Clientes</title>
5. </head>
6. <body>
7. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
8. </body>
9. </html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Clientes</title>
</head>
<body>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</body>
</html>