← Desenvolvimento

Chamar, regularmente, uma página

Lida 2986 vezes

Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Boa noite.
Preciso de chamar uma página regularmente dentro de um site.

Actualmente utilizo este método:
Tenho uma frame (0px x 0px) com um meta refresh a cada 30 minutos, que actualiza a página www.site.com/alert.php dentro da frame. Essa página "alert" está em branco, e vai servir para fazer lançar um JS Alert quando eu trocar essa página por uma com o código do JS Alert, daí ter que actualizar regularmente.

No entanto, este sistema é mau porque a página demora algum tempo a carregar e por fazer aquele barulho característico de uma página a abrir quando é visto através do IE, por exemplo.

Existe alguma maneira de fazer isto com outra tecnologia como Ajax, JavaScript, etc?

Obrigado ;)
Offline

Bruno Mota 
Membro
Mensagens 1733 Gostos 3
Troféus totais: 28
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

pesquisa por divrefresh
Offline

STronic 
Elite
Mensagens 546 Gostos 8
Feedback +5

Troféus totais: 30
Trófeus: (Ver todos)
Mobile User Search Apple User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Ficheiro ajax.js
Código: (javascript) [Seleccione]
// Customise those settings

var seconds = 900;
var divid = "refreshdiv";
var url = "refresh.php";

////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////

function refreshdiv(){

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}

Página refresh.php
Código: (php) [Seleccione]
<?php
if ($variavel != '') { 
echo('O que queres mostrar se existe a variavel');
} else { 
 echo('<div style="display:none"><iframe src="/extras/alert.php" width="0" height="0"></iframe></div>');
}
?>


Página alert.php
Código: (php) [Seleccione]
<script language="javascript">alert("O alert que quiseres\nLinha2...")</script>
Página blablabla.php (onde queres a DIV que faça o refresh)
Código: (php) [Seleccione]
<span id="refreshdiv"></span>


Eu tenho assim a funcionar num site ;)
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Vou experimentar.

Chamo o Ajax na página onde também chamo a DIV?
Offline

STronic 
Elite
Mensagens 546 Gostos 8
Feedback +5

Troféus totais: 30
Trófeus: (Ver todos)
Mobile User Search Apple User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Vou experimentar.

Chamo o Ajax na página onde também chamo a DIV?

sim ;)
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

sim ;)
Ah, não estava a funcionar porque não reparei que tinhas posto /extras/

Agora está perfeito. Obrigado ;)
Offline

STronic 
Elite
Mensagens 546 Gostos 8
Feedback +5

Troféus totais: 30
Trófeus: (Ver todos)
Mobile User Search Apple User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Ah, não estava a funcionar porque não reparei que tinhas posto /extras/

Agora está perfeito. Obrigado ;)

Pois... editei alguma coisa, mas não tirei isso .. ;)
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Pois... editei alguma coisa, mas não tirei isso .. ;)
Sem problema :)

Aproveito para dizer que peguei num JS, que já tinha do formato anterior, que grava num cookie o facto de o alerta já ter sido lançado, assim, quando a página fizer reload e o utilizador já tiver visto o alerta, não o verá novamente.

Se alguém quiser envie PM que eu procuro o site de onde tirei ;)
Offline

STronic 
Elite
Mensagens 546 Gostos 8
Feedback +5

Troféus totais: 30
Trófeus: (Ver todos)
Mobile User Search Apple User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Sem problema :)

Aproveito para dizer que peguei num JS, que já tinha do formato anterior, que grava num cookie o facto de o alerta já ter sido lançado, assim, quando a página fizer reload e o utilizador já tiver visto o alerta, não o verá novamente.

Se alguém quiser envie PM que eu procuro o site de onde tirei ;)

Podias colar aqui ;) fica o sistema completo :P