Common JavaScript behaviours for web pages
Another variant of my JavaScript library, this time a very tiny script. I've put it in the database as a Shared code > Script library. A great place to put it, because you have syntax highlighting and syntax error preventing. One thing I noticed though: when you use too advanced regular expressions, the error preventing goes havoc and throws errors where there are none.
The script
/* common javascript */
var G={w:window,d:document,init:function(){for(var n in G)if(G[n].init)G[n].init()}},
$=function(a,b){return (b||G.d).getElementById(a)},
$$=function(a,b){return (b||G.d).getElementsByTagName(a)}
G.w.onload=G.init
G.common={
init:function(){
var m=this,o=$$('a'),i
for(i=0;i<o.length;i++){
if(o[i].className=='external')m.popup(o[i])
if(o[i].className=='mailto')m.mailto(o[i])
}
o=$$('table')
for(i=0;i<o.length;i++){
if(o[i].className=='stripe')m.stripe(o[i])
}
},
popup:function(a){
a.title='New window: '+(a.title||a.href)
a.onclick=function(){window.open(a.href)}
return false
},
mailto:function(a){
var v=decodeURI(a.href).replace(/ AT /,'@').replace(/ DOT /g,'.')
a.href=v
a.title='Send e-mail to: '+(a.title||v.split('mailto:')[1].split('@')[0])
},
stripe:function(a){
var o=$$('tr',a),i
for(i=0;i<o.length;i+=2)o[i].className='odd'
}
}
The CSS
/* popup, mailto and stripe */
.external{background:url(icon_popup.gif) right no-repeat;padding-right:15px}
.mailto{background:url(icon_mail.gif) center right no-repeat;padding-right:18px}
.odd{background:#eef}
Wich uses these two images:
and
.
What it does
- Links with className 'external' are opened in a popup window.
- Links with className 'mailto' can be obfuscated, replacing @ with ' AT ' and a dot with ' DOT ' to avoid them being picked up by spammers. The script translate them back to normal e-mail addresses.
- Tables with className 'stripe' are, well... striped. You do need to provide the background color in a class named 'odd' in your stylesheet.
The result
Here is the link to the website in its current state.
Comments
To add a comment, log in or register as new user. It's free and safe.