Newsgroup: it.comp.lang.javascript
Titolo: Forzare il reload

Salve a tutti.
Da qualche mese utilizzo questo newsgroup per cercare soluzioni ai miei
problemi con javascript... e le trovo!
Finalmente ho risolto un problema che mi angustiava da tempo: forzare
il reload di una pagina *SOLO* agli utenti che hanno visto una versione
non aggiornata della stessa, non obbligando quindi chi la visita spesso
a noiosi e lunghi caricamenti.
Qui sotto il codice.
Ciao a tutti/e
Cally Penzucci.
<pre>
<html>
<head>
<title>Cookie Functions</title>
<script language="javascript">
<!-- begin script
//This function get the value of the cookies variables
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//Read the cookie
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
//Create a cookie
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
//Delete the cookie by setting the expires date to 1 Jan 1970
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
/*
Function created by Matteo Penzo
mail: 
Pe...@peb-web.com
Internet: 
http://www.peb-web.com
You can freely use and modified this function as long you leave the
above lines unmodified.
This function first controls if there is a cookie named trano.
a)If not it creates a cookie with inside the version of the page the
user is loading.
b)If a cookie is present the function compares the date in the cookie
with the last modified
date of the page; if they're different it means that the page has
changed since the user
visited it and so it loads the new version.
Don't hesitate to contact me if you have any question
*/
function controllo()
{
var lastVisit = GetCookie("trano")
if (lastVisit==null) //there isen't a cookie
{
	SetCookie ("trano", document.lastModified , null, null, null,
null);
	window.location.href=window.location.href;
}
else //The function has found a cookie
{
var htmlOut = lastVisit;
year=htmlOut.substring(6,10)
month=htmlOut.substring(3,5)
day=htmlOut.substring(0,2)
hour=htmlOut.substring(11,13)
minute=htmlOut.substring(14,16)
//The date in the cookie
biscottino=year+month+day+hour+minute
//The date of the last modify of the page
modifica=document.lastModified.substring(6,10)
+document.lastModified.substring(3,5)+document.lastModified.substring
(0,2)+document.lastModified.substring(11,13)
+document.lastModified.substring(14,16)
//here you can control if it's all ok:
//the two variables are equal if nothing has changed otherwise the page
is reloaded and
//"then" they're equalized.
//****************************************
//**delete this to use in your web pages**
//****************************************
alert(biscottino+"\n"+modifica)
if(biscottino<modifica)
{
DeleteCookie("trano", null, null);
SetCookie ("trano", document.lastModified , null, null, null, null);
window.location.href=window.location.href; //this command reloads the
page
}
else document.write("")
}
}
// end script -->
</script>
</head>
<body>
<script>
controllo();
</script>
</body>
</html>
</pre>
--
http://www.PeB-WEB.com
FREE newsletter, articles and tips about webdesigning, promotion
and search engines positioning
Sent via Deja.com 
http://www.deja.com/
Share what you know. Learn what you don't.

Ciao
Grazie dello script!
--
Bye
Ro<*¿*>berto


--
http://www.PeB-WEB.com
FREE newsletter, articles and tips about webdesigning, promotion and sea


prova con:
var lastVisit = new Date(GetCookie("trano"));
--
----
