Variabili globali con javascript

In questo breve articolo vedremo un trucco per usare le variabili globali con javascript.
Sappiamo che in javascript possiamo definire una variabile semplicemente con:

 
var unavariabile="valore";
 

cosi facendo il suo ambito (scope) è solo nell'esecuzione del programma e non può essere modificato all'interno di una funzione. Per esempio:

 
function modifica(){
unavariabile="un altro valore";
}
 

non altererà il valore della variabile unavariabile ma lo modificherà solo all'interno del corpo della funziona modifica.
Per sbrogliare questa matassa dobbiamo usare un trucco:

 
var testing = { unavariabile: 'questo valore lo cambia'}
 

basta che ci riferiamo alla variabile testing.unavariabile la possiamo usare come assoluta, infatti la possiamo cambiare anche all'interno del corpo della funzione:

 
function modifica(){
testing.unavariabile="un altro valore";
}
 
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Javascript Password Strength Meter

Javascript Password Strength Meter è un file javascript che ci permette di verificare se la nostra password è piu o meno sicura.

Sito web: http://www.geekwisdom.com/dyn/passwdmeter
Js: http://www.geekwisdom.com/js/passwordmeter.js

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

15000 tutorials per photoshop

Ottima raccolta di tutorials su good-tutorials.com divisi per categorie.

Sito web: http://www.good-tutorials.com/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Multilevel Drop Down Navigation Menus: Esempi e Tutorials



Sito web: http://www.noupe.com/..-tutorials.html

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Tutorial avanzati su tematiche web su nettuts.com

Ottimo sito dove poter trovare interessanti tutorial avanazati su programmazione web.

Sito web: http://nettuts.com/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Modificare lo stile di una pagina web modificando il file css con javascript

In questo articolo vedremo come modificare lo stile css di una pagina web con uno script javascript. Nella pagina html, avremo l'elenco degli stili:

...

<link rel="stylesheet" type="text/css" href="stile1.css" title="stile1" />
<link rel="alternate stylesheet" type="text/css" href="stile2.css" title="stile2" />

... 

e oltre al resto della pagina, la chiamata al file javascript:

 <script type="text/javascript" src="switcher.js"></script>

cosi impostato:

/* Style Switcher by Paul Sowden, see A List Apart: http://www.alistapart.com/articles/alternate/ */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

document.writeln("<div id=\"textsize\">Passa a stile1: <a href=\"#\" onclick=\"setActiveStyleSheet('stile1'); return false;\" id=\"stile1\">Stile1</a><a href=\"#\" onclick=\"setActiveStyleSheet('stile2'); return false;\" id=\"stile2\">Passa a stile2</a></div>"); 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

bananascript.com comprime file javascript

 

 Se volete comprimere file javascript, bananascript.com fà al caso vostro. Permette di ridurre di tanto la dimensione di un file javascript.

Sito web: http://www.bananascript.com/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Mostrare un elemento div alle coordinate del mouse

In questo articolo vedremo come mostrare un elemento div alle coordinate del mouse. Supponiamo di avere un div con id divColor in posizione 155,355, e vogliamo che prenda la posizione del mouse

function ShowDivAtPoint(id){
     var x=event.x; //Cordinata X del Mouse
     var y=event.y; //Cordinata Y del Mouse
     var doc=document.getElementById(id);
     doc.style.left=x;
     doc.style.top=y;
}

che richiameremo con

ShowDivAtPoint('divColor') 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Evidenziare il testo presente in una pagina con javascript

 

In questo tutorial vedremo come evidenziare delle parole di un testo direttamente da javascript, passandogli attraverso la querystring al parola da evidenziare.

File contenente il testo 

Come prima cosa creiamo o modifichiamo il nostro file che per comodità ho preso essere html. Definiamo una classe css che setta lo stile dell'evidenziamento:

<style>
.searchword{
background-color:yellow;
}
</style>

e poi richiamiamo lo script javascript che si occuperà di fare tutto il lavoro:

<script src="searchhi.js"></script> 

File javascript 

/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modificato 19 gennaio 2008 da sandro stracuzzi - sandrostracuzzi@hotmail.com */
function highlightWord(node,word) {
        // Iterate into this nodes childNodes
        if (node.hasChildNodes) {
                var hi_cn;
                for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
                        highlightWord(node.childNodes[hi_cn],word);
                }
        }
        // And do this node itself
        if (node.nodeType == 3) { // text node
                tempNodeVal = node.nodeValue.toLowerCase();
                tempWordVal = word.toLowerCase();
                if (tempNodeVal.indexOf(tempWordVal) != -1) {
                        pn = node.parentNode;
                        // check if we're inside a "nosearchhi" zone
                        checkn = pn;

                        while (checkn.nodeType != 9 &&
                        checkn.nodeName.toLowerCase() != 'body') {
                        // 9 = top of doc
                                if (checkn.className.match(/\bnosearchhi\b/)) { return; }
                                checkn = checkn.parentNode;
                        }
                        if (pn.className != "searchword") {
                                // word has not already been highlighted!
                                nv = node.nodeValue;
                                ni = tempNodeVal.indexOf(tempWordVal);
                                // Create a load of replacement nodes
                                before = document.createTextNode(nv.substr(0,ni));
                                docWordVal = nv.substr(ni,word.length);
                                after = document.createTextNode(nv.substr(ni+word.length));
                                hiwordtext = document.createTextNode(docWordVal);
                                hiword = document.createElement("span");
                                hiword.className = "searchword";
                                hiword.appendChild(hiwordtext);
                                pn.insertBefore(before,node);
                                pn.insertBefore(hiword,node);
                                pn.insertBefore(after,node);
                                pn.removeChild(node);
                       }
                }
        }
}

function googleSearchHighlight() {
var q = location.search;
var q = q.replace("?q=","")
highlightWord(document.getElementsByTagName("body")[0],q);
}

window.onload = googleSearchHighlight;

Funzionamento 

Basta richiamare il nome del file passandogli alla variabile q nella querystring il valore da cercare e il gioco è fatto. Ad esempio:

ricerca.html?q=prova

Dove ricerca,html è il file html di cui parlavamo all'inizio 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati

Internalizionalizzazione con javascript

 

Sito web: http://24ways.org/2007/javascript-internationalisation

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati