Never been to TextSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Toggle Show/Hide
var u = document.getElementById(ul_id);
if(u.className == "hiddenAnswer") {
u.className = null;
} else {
u.className = "hiddenAnswer";
}
}
// then create a css class like so...
.hiddenAnswer {
display: none;
}
Javascript Popup Function
function popUp(url,added) { menubar = "no"; if (added == "nav") {menubar = "yes";} if (typeof(popupWin) != "object"){ popupWin = window.open(url,"gmail" , "height=400,width=600,toolbar=" + menubar + ",scrollbars=yes,personalbar=yes,resizable=yes,location=0,statusbar=yes,menubar=0"); } else { if (!popupWin.closed){ popupWin.location.href = url; } else { popupWin = window.open(url, "gmail" , "height=400,width=600,toolbar=" + menubar + ",scrollbars=yes,personalbar=yes,resizable=yes,location=0,statusbar=yes,menubar=0"); } } popupWin.focus(); return false; }
Simple DIV Toggle Javascript
function simpletogglediv(whichLayer) { var theElementStyle = document.getElementById(whichLayer); if(theElementStyle.style.display == "block") { theElementStyle.style.display = "none"; } else { theElementStyle.style.display = "block"; } }
Fireworks Javascript image functions
function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} }
Javascript optimisation - while
var i = 0, el, els = document.getElementsByTagName(nodeName); while (el = els[i++]) { // [...] }
Using a while loop and storing the live collection in a variable is the quickest technique in all of these browsers.
Using FlashObject and the miniFLV player to play videos from an entry
This code assumes custom fields in the blog.
FlashObject: http://blog.deconcept.com/flashobject/
MiniFLV: http://www.richnetapps.com/miniflv.htm
Online Example.
{if project_video} {if project_video_title}<h4>{project_video_title}: Video Cliph4>{/if} <div id="videoClip"> <p style="margin: 10px 0; padding: 10px; border: 3px solid red;"><a href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank" title="Get Flash Player"><img src="http://www.macromedia.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Flash Player" height="31" width="88" align="left" style="padding-right: 10px">a>Flash 7 or above and JavaScript are required to view the project video clip. Please download the //www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank" title="Get Flash Player">latest Flash plug-in. {if project_video_alt}{project_video_alt}
{/if}
Scroll automatically to the bottom of a div
var objDiv = document.getElementById("divExample"); objDiv.scrollTop = objDiv.scrollHeight;
Forward, Top & Back
<a href="javascript:history.back();">Back A Pagea> | Back to Top</a> | <a href="javascript:history.forward();">Forward A Pagea>
clear default text onClick - restore if nothing entered
http://www.scriptygoddess.com/archives/2005/11/15/clear-default-text-onclick-restore-if-nothing-entered/
In your document's head:
<script type="text/javascript"> function clickclear(thisfield, defaulttext) { if (thisfield.value == defaulttext) { thisfield.value = ""; } } function clickrecall(thisfield, defaulttext) { if (thisfield.value == "") { thisfield.value = defaulttext; } } script>
The actual form field:
<input type="text" name="myfield" onclick="clickclear(this, 'default text')" onblur="clickrecall(this,'default text')" />
Easy links to/from glossary terms with JavaScript
Also somewhat old and probably not nearly as efficient as it could be, so ideas for improvement would be welcome.
function createLinkedFootnotes() { defs = document.getElementsByTagName('dfn'); for(i = 0; i < defs.length; i++) { num = i + 1; footnote = document.createElement('sup'); linkToDef = document.createElement('a'); anchor = '#def' + num; termID = 'term' + num; linkToDef.setAttribute('href', anchor); linkToDef.setAttribute('id', termID); linkToDef.appendChild(document.createTextNode(num)); footnote.appendChild(linkToDef); defs[i].appendChild(footnote); } terms = document.getElementsByTagName('dt'); for(i = 0; i < terms.length; i++) { num = i + 1; footnote = document.createElement('sup'); linkBack = document.createElement('a'); anchor = '#term' + num; defID = 'def' + num; linkBack.setAttribute('href', anchor); linkBack.setAttribute('id', defID); linkBack.appendChild(document.createTextNode(num)); footnote.appendChild(linkBack); terms[i].insertBefore(footnote, terms[i].firstChild); } }
Clear form with Javascript
Other important features:
- Resulting page must be valid XHTML 1.1
- JS must work in Firefox 1.0 and MS Internet Explorer 6.0
- JS must never generate any errors, even non-fatal ones, in the Firefox Javascript Console
- JS should only clear text input (no hidden fields or visual controls)
- Code must be readable and properly indented
Looking around the web, this was not easy to find. After a few hours of searches, and some back'n'forth at Webmaster World, I got a working version.
In XHTML header:
<script type="text/javascript" src="clear-form-input.js">script>
In html/form/div
<script type="text/javascript"> // CDATA[ var el = document.createElement("input"); el.setAttribute("id", "clearButton"); el.setAttribute("type", "button"); el.setAttribute("value", "Clear"); document.getElementById("deliciousFormDiv").appendChild(el); addEvent(el, 'click', function(){ clearForm('delicious');} ); // ]]> script>
In clear-form-input.js:
function clearForm(formIdent) { var form, elements, i, elm; form = document.getElementById ? document.getElementById(formIdent) : document.forms[formIdent]; if (document.getElementsByTagName) { elements = form.getElementsByTagName('input'); for( i=0, elm; elm=elements.item(i++); ) { if (elm.getAttribute('type') == "text") { elm.value = ''; } } } // Actually looking through more elements here // but the result is the same. else { elements = form.elements; for( i=0, elm; elm=elements[i++]; ) { if (elm.type == "text") { elm.value =''; } } } } function addEvent(elm, strEvent, fnHandler) { return ( elm.addEventListener ? elm.addEventListener( strEvent, fnHandler, false) : elm.attachEvent( 'on'+strEvent, fnHandler) ); }
Back to Top
<a href="#" onclick="window.scrollTo(0,0); return false">Back to Topa>
JS translation bookmarklet
javascript:langpair="fr|en"; text=""+(window.getSelection?window.getSelection() :document.getselection?document.getSelection() :document.selection.createRange().text); if(!text)text=prompt("enter%20text%20to%20translate",""); if(text!=null)void(window.open( "http://google.com/translate_t?langpair="+langpair+"&text="+text, "translate","scrollbars=1,resizablel=1,width=500,height=500"))
Back and Forward Links in History
<form> <input type="button" value="Back" onclick="history.back()"> <input type="button" value="Forward" onclick="history.forward()"> <input type="button" value="Reload" onclick="location.reload()"> form>
Using a normal link:
<a href="javascript:history.back();">Backa>
Redirect after 5 seconds
<HTML> <HEAD> <TITLE>RedireccionadoTITLE>