Foros del Web » Programando para Internet » Javascript »

Hash: No entiendo el script

Estas en el tema de Hash: No entiendo el script en el foro de Javascript en Foros del Web. Hola estoy intentando comprender un código de una pagina,sobre pasarle el hash con # tal y como hace tuenti,y que luego haga una petición AJAX,entiendo ...
  #1 (permalink)  
Antiguo 23/11/2010, 06:35
 
Fecha de Ingreso: septiembre-2010
Mensajes: 43
Antigüedad: 13 años, 7 meses
Puntos: 0
Hash: No entiendo el script

Hola estoy intentando comprender un código de una pagina,sobre pasarle el hash con # tal y como hace tuenti,y que luego haga una petición AJAX,entiendo casi todo el código pero no entiendo una cosa,que es lo que hace la funcion verify_hash() y la variable page_timer,dejo aqui el script y la pagina donde lo saco:
A ver si podeis ayudarme,gracias
Cita:
/* AJAX request example with hashes */
/* by Adrián Navarro (http://adrian.navarro.at) */
/* <[email protected]> */

/* Monitors the URL for a hash, and loads the */
/* content through a standard AJAX request using */
/* the jQuery framework. */

/* Useful for heavy '2.0' applications using lots */
/* of AJAX. This also enables the end-user to use */
/* the "back" button from his browser. */

var default_hash = null;
var error_timer = null;
var page_timer = null;

var error_timeout = 12000;
var timer_wait = 500;

function launch() {
/* should do:
1. set the page load timer
2. set the error throw timer (went wrong, stop everything, retry)

also check if there's an hash, send it

first time we load:
- get the hash (there's?), send it through ajax. use std functions
*/

load_page(window.location.hash);
}

function load_page(hash) {
/*
most important function: load the webpage (through ajax)
- sets an error timer that will be removed if it's loaded before 'error_timeout' in msec
- sets the default hash to the hash we are going to work with (for verifyhash() interval)
- create an AJAX request url (formality...)
- then throw AJAX request and redirect it to load_page_success function
*/

error_timer = setTimeout("declare_error()", error_timeout);

default_hash = hash;
hash = clean_hash(hash);
if(hash.length > 0) hash = "?" + hash;

$.ajax({type: "GET", url: "pageloader.php" + hash, success: load_page_success});
}

function clean_hash(hash) {
/*
this function removes the # if given in the 'hash' string
some browsers include this in the window.location.hash, dunno
*/

new_hash = hash.replace(/^\#/, "");
return new_hash;
}

function declare_error() {
/*
if "shit happens": (yeah, it often does..)
- clear all timers
- hide all shown (or not) divs
- show the awesome 'error' div
*/

if(error_timer) clearTimeout(error_timer);
if(page_timer) clearTimeout(page_timer);

document.getElementById('nojs').style.display='non e';
document.getElementById('jscontent').style.display ='none';
document.getElementById('error').style.display='';
}

function load_page_success(html) {
/*
we've got: html
also, we have to remove the error timer and re-activate the hash checking timer
*/

clearTimeout(error_timer);
$("#jscontent").html(html);
page_timer = setTimeout("verify_hash()", timer_wait);
}

function verify_hash() {
/*
checks if we haven't loaded the webpage already
then fires loadpage() to load through ajax

also re-initiates (in a loop) the verify_hash timer
*/

page_timer = setTimeout("verify_hash()", timer_wait);

if(default_hash == window.location.hash) return;
load_page(window.location.hash);
}
Fuente: http://adrian.navarro.at/demo/hash/scripts.js
  #2 (permalink)  
Antiguo 24/11/2010, 09:09
 
Fecha de Ingreso: septiembre-2010
Mensajes: 43
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Hash: No entiendo el script

Bueno aunque nadie ha respondido creo que me he dado cuenta de lo que hace page_timer, creo que lo que hace es contar el tiempo que esta la pagina,y autorefrescarse cada cierto tiempo para que envie la peticion al servidor por AJAX,y de ese modo se vaya actualizando

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:33.