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!)

« Newer Snippets
Older Snippets »
Showing 61-80 of 81 total

Collapsable Navigation Menu

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">







Example 2










Toggle Show/Hide

function toggleAnswer(ul_id) {
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

// javascript popup

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

// description of your code here

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

// rollover, swap, etc.

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

Using as a benchmark the task of iterating over a live collection produced by getElementsByTagName. IE/Win, Gecko and Safari all agree as to the fastest means:

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

Ideally, at some point, I'll modify the plug-in for EE that shows Flash objects to use flashobject.js.

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}
{/if}

Scroll automatically to the bottom of a div

var objDiv = document.getElementById("divExample");
objDiv.scrollTop = objDiv.scrollHeight;

clear default text onClick - restore if nothing entered

From ScriptyGoddess:
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

Quick unobtrusive JavaScript that provides links to and from terms defined in a glossary in your page. Assumes you mark up terms to define with dfn, and put the glossary in a definition list. Currently needs terms in the definition list to appear in the same order as they do in the main text.

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

Purpose: Clear all text fields in a form

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

I got this mostly working - the only thing that sucks is encoding of non-ascii characters like �, �, etc... I've tried escape(), but it's just stripped the offending character. Anyone know js and feel like fixing this?

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"))

Javascript Image Preload

 

Back and Forward Links in History

Using form buttons:

<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>