Notis

Síguenos en Twitter: @teoria_blogger | Para tu blog, recomendamos: que el diseño no esté cargado, que las entradas no sean muy largas y que contengan imágenes. Para más consejos Click aquí | Pon un link de Teoría Blogger en tu blog!!! Click aquí. | Para cualquier duda o problema sobre tu blog, escríbenos en nuestro formulario de contacto.

sábado, 14 de agosto de 2010

Haz que tu blog salude a un visitante con su nombre

Este truco está relacionado con las cookies de tu navegador. En donde al entrar a tu blog el visitante deberá introducir su nombre o un nick y luego en la columna de gadgets se muestra un mensaje que saluda al visitante con el nick ingresado. Ver el truco en el Laboratorio de T.B.


En esta imagen se muestra el cuadro para escribir el nombre al entrar el blog.


Ahí se muestra el saludo en el gadget con el nombre incluido.
El código se debe pegar en el mismo gadget. O sea que vasmos a Blogger → Diseño → Elementos de la página → Añadir un gadget → HTML/Javascript y pegamos este código (siempre en la solapa Edición de HTML):

<script>
<!--

// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false

// nombre - Nombre para el cookie
// value - value of the cookie
// [expiracion] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(nombre, value, expiracion, path, domain, secure) {
var curCookie = nombre + "=" + escape(value) +
((expiracion) ? "; expiracion=" + expiracion.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (nombre + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}

// nombre - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(nombre) {
var prefix = nombre + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// nombre - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(nombre, path, domain) {
if (getCookie(nombre)) {
document.cookie = nombre + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expiracion=Thu, 01-Jan-70 00:00:01 GMT"
}
}

// date - any instance of the Date object
// * you should hand all instances of the Date object to this function for "repairs"
// * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}

var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000)
var nombre = getCookie("nombre")
if (!nombre)
nombre = prompt("Ingrese un nick o nombre:", "Internauta")
setCookie("nombre", nombre, now)
document.write("Hola " + nombre + "!")
//-->
</script>
Como es un truco muy simple. No se necesita agregar ninguna modificación.
Haz la prueba verás que es un truco no muy visto en páginas webs y es muy curioso.

0 comentarios:

Publicar un comentario

Podés utilizar códigos HTML como <a><b><i>.
Si tenés alguna duda, no dudes en recurrir a nuestro Formulario de contacto.