<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Bradalyst's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 01:06:02 GMT</pubDate>
    <description>DZone Snippets: Bradalyst's Code Snippets</description>
    <item>
      <title>Unobtrusively Executing JavaScript on Page Load</title>
      <link>http://snippets.dzone.com/posts/show/4643</link>
      <description>http://simonwillison.net/2004/May/26/addLoadEvent/&lt;br /&gt;&lt;br /&gt;The addLoadEvent function takes as an argument another function which should be executed once the page has loaded. Unlike assigning directly to window.onload, the function adds the event in such a way that any previously added onload functions will be executed first.&lt;br /&gt;&lt;br /&gt;The way this works is relatively simple: if window.onload has not already been assigned a function, the function passed to addLoadEvent is simply assigned to window.onload. If window.onload has already been set, a brand new function is created which first calls the original onload handler, then calls the new handler afterwards.&lt;br /&gt;&lt;br /&gt;addLoadEvent has one very important property: it will work even if something has previously been assigned to window.onload without using addLoadEvent itself. This makes it ideal for use in scripts that may be executing along side other scripts that have already been registered to execute once the page has loaded.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function addLoadEvent(func) {&lt;br /&gt;  var oldonload = window.onload;&lt;br /&gt;  if (typeof window.onload != 'function') {&lt;br /&gt;    window.onload = func;&lt;br /&gt;  }&lt;br /&gt;  else {&lt;br /&gt;    window.onload = function() {&lt;br /&gt;      if (oldonload) {&lt;br /&gt;        oldonload();&lt;br /&gt;      }&lt;br /&gt;      func();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);&lt;br /&gt;addLoadEvent(function() {&lt;br /&gt;  /* more code to run on page load */ &lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 16:51:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4643</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>HTML/JavaScript - Select list - Add/Remove Options (DOM)</title>
      <link>http://snippets.dzone.com/posts/show/4442</link>
      <description>from http://www.mredkj.com/tutorials/tutorial005.html&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Overview&lt;br /&gt;&lt;br /&gt;    * Insert Before Selected - A new option is created and added above the selected option (as determined by selectedIndex). If none are selected, then no option is added.&lt;br /&gt;    * Remove Selected - Deletes the selected option (or options) from the list. If no options are selected, no options are deleted.&lt;br /&gt;    * Append Last - No matter what is selected, a new option is added at the end.&lt;br /&gt;    * Remove Last - No matter what is selected, the last option is deleted from the list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Explanation&lt;br /&gt;&lt;br /&gt;According to DOM Level 1, the following is the syntax for the add and remove methods in HTMLSelectElement:&lt;br /&gt;&lt;br /&gt;void add(in HTMLElement element, in HTMLElement before) raises(DOMException);&lt;br /&gt;void remove(in long index);&lt;br /&gt;&lt;br /&gt;The add method takes two arguments: the element to add, and the element to insert before. The spec also says you can add to the end of the list by passing null as the second argument.&lt;br /&gt;&lt;br /&gt;The remove method just takes a number: the index of the option to be removed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The JavaScript&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script language="JavaScript" type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;var count1 = 0;&lt;br /&gt;var count2 = 0;&lt;br /&gt;&lt;br /&gt;function insertOptionBefore(num)&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.selectedIndex &gt;= 0) {&lt;br /&gt;    var elOptNew = document.createElement('option');&lt;br /&gt;    elOptNew.text = 'Insert' + num;&lt;br /&gt;    elOptNew.value = 'insert' + num;&lt;br /&gt;    var elOptOld = elSel.options[elSel.selectedIndex];  &lt;br /&gt;    try {&lt;br /&gt;      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE&lt;br /&gt;    }&lt;br /&gt;    catch(ex) {&lt;br /&gt;      elSel.add(elOptNew, elSel.selectedIndex); // IE only&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionSelected()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  var i;&lt;br /&gt;  for (i = elSel.length - 1; i&gt;=0; i--) {&lt;br /&gt;    if (elSel.options[i].selected) {&lt;br /&gt;      elSel.remove(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function appendOptionLast(num)&lt;br /&gt;{&lt;br /&gt;  var elOptNew = document.createElement('option');&lt;br /&gt;  elOptNew.text = 'Append' + num;&lt;br /&gt;  elOptNew.value = 'append' + num;&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE&lt;br /&gt;  }&lt;br /&gt;  catch(ex) {&lt;br /&gt;    elSel.add(elOptNew); // IE only&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionLast()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    elSel.remove(elSel.length - 1);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;//--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The HTML&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="insertOptionBefore(count1++);" /&gt;&lt;br /&gt;Insert Before Selected&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionSelected();" /&gt;&lt;br /&gt;Remove Selected&lt;br /&gt;&lt;br /&gt;&lt;select id="selectX" size="10" multiple="multiple"&gt;&lt;br /&gt;&lt;option value="original1" selected="selected"&gt;Orig1&lt;/option&gt;&lt;br /&gt;&lt;option value="original2"&gt;Orig2&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="appendOptionLast(count2++);" /&gt;&lt;br /&gt;Append Last&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionLast();" /&gt;&lt;br /&gt;Remove Last&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 22 Aug 2007 12:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4442</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>Automatic Breadcrumbs</title>
      <link>http://snippets.dzone.com/posts/show/4427</link>
      <description>From: http://hyperdisc.unitec.ac.nz/materials/javascript/top10/breadcrumbs.htm&lt;br /&gt;&lt;br /&gt;It is possible to hard-code the links in a breadcrumb trail - i.e. to add all the links manually. It is also possible to automatically produce a breadcrumb trail for all pages. This can be done server-side using XSSI, ASP, ColdFusion or PHP. It can also be done client-side using JavaScript.&lt;br /&gt;&lt;br /&gt;This is working at the top of every page in the Materials site.&lt;br /&gt;&lt;br /&gt;You need to add the function JavaScript to the HEAD of every page and the call to the function in the BODY of every page, wherever you would like the breadcrumb trail to appear. You can add the call to the function multiple times in the same page.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;HEAD&gt;&lt;br /&gt;&lt;script language="JavaScript"&gt;&lt;br /&gt;&lt;!-- &lt;br /&gt;  function breadcrumbs(){&lt;br /&gt;    sURL = new String;&lt;br /&gt;    bits = new Object;&lt;br /&gt;    var x = 0;&lt;br /&gt;    var stop = 0;&lt;br /&gt;    var output = "&lt;div class=topnav&gt;&lt;A HREF=/&gt;Hyperdisc&lt;/A&gt; &amp;raquo; ";&lt;br /&gt;&lt;br /&gt;    sURL = location.href;&lt;br /&gt;    sURL = sURL.slice(8,sURL.length);&lt;br /&gt;    chunkStart = sURL.indexOf("/");&lt;br /&gt;    sURL = sURL.slice(chunkStart+1,sURL.length)&lt;br /&gt;&lt;br /&gt;    while(!stop){&lt;br /&gt;      chunkStart = sURL.indexOf("/");&lt;br /&gt;      if (chunkStart != -1){&lt;br /&gt;        bits[x] = sURL.slice(0,chunkStart)&lt;br /&gt;        sURL = sURL.slice(chunkStart+1,sURL.length);&lt;br /&gt;      }else{&lt;br /&gt;        stop = 1;&lt;br /&gt;      }&lt;br /&gt;      x++;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    for(var i in bits){&lt;br /&gt;      output += "&lt;A HREF=\"";&lt;br /&gt;      for(y=1;y&lt;x-i;y++){&lt;br /&gt;        output += "../";&lt;br /&gt;      }&lt;br /&gt;      output += bits[i] + "/\"&gt;" + bits[i] + "&lt;/A&gt; &amp;raquo; ";&lt;br /&gt;    }&lt;br /&gt;    document.write(output + document.title);&lt;br /&gt;	document.write("&lt;/div&gt;");&lt;br /&gt;  }&lt;br /&gt; // --&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/HEAD&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;BODY&gt;&lt;br /&gt;&lt;br /&gt;&lt;SCRIPT LANGUAGE="JavaScript" TYPE='text/javascript'&gt;breadcrumbs()&lt;/SCRIPT&gt;&lt;br /&gt;&lt;br /&gt;&lt;/BODY&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Points to note about this version of the script:&lt;br /&gt;&lt;br /&gt;    * It wraps the entire breadcrumb trail in a CSS class called "topnav". You can style this however you like, or you could modify the script to change 'topnav' to any CSS class name you want.&lt;br /&gt;    * It names the site "Hyperdisc". You need to modify the script to change the name to something else.&lt;br /&gt;    * It assumes that the homepage of the site is in the root directory of the web server. If the homepage of your site is actually in a sub-directory, you would need to seriously modify the script.&lt;br /&gt;    * You need to have an index.htm (or equivalent) file in each directory or the user will experience 403 errors.&lt;br /&gt;    * Apparantly this script causes errors in older browsers.&lt;br /&gt;    * Also note that this script does not work in frames.&lt;br /&gt;</description>
      <pubDate>Fri, 17 Aug 2007 13:18:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4427</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>javascript - dom method for replacing nodes</title>
      <link>http://snippets.dzone.com/posts/show/2599</link>
      <description>In addition to removing nodes, the DOM offers a way to replace one node with another. The replaceChild method accomplishes this. As with removeChild above, replaceChild needs to be called from the element that contains the node you wish to replace.&lt;br /&gt;&lt;br /&gt;replaceChild takes two arguments: a reference to the new node, and another to the node being replaced. The following example creates a new SPAN element containing a text node, and uses it to replace an existing SPAN. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;function replaceSpan(){&lt;br /&gt;&lt;br /&gt;    var newSpan = document.createElement("span");&lt;br /&gt;    var newText =&#172; &lt;br /&gt;	document.createTextNode("on top of the astounded zebra");&lt;br /&gt;    newSpan.appendChild(newText);&lt;br /&gt;&lt;br /&gt;    var para = document.getElementById("example3");&lt;br /&gt;    var spanElm = document.getElementById("ex3Span");&lt;br /&gt;    var replaced = para.replaceChild(newSpan,spanElm);&lt;br /&gt;}&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p id="example3"&gt;The quick brown fox jumps &lt;span id="ex3Span"&gt;over the lazy dog&lt;/span&gt;.&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;button onclick="replaceSpan();"&gt;Call replaceSpan()&lt;/button&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Sep 2006 22:53:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2599</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>javascript - dom method for removing nodes</title>
      <link>http://snippets.dzone.com/posts/show/2598</link>
      <description>You can remove existing nodes as well as add new ones. The removeChild method allows any node to remove one of its child nodes. Simply pass a reference to the node you wish to remove. Any text or elements within the node being removed will be removed along with it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;&lt;br /&gt;function removeBElm(){&lt;br /&gt;&lt;br /&gt;    var para = document.getElementById("example2");&lt;br /&gt;&lt;br /&gt;    var boldElm = document.getElementById("example2B");&lt;br /&gt;&lt;br /&gt;    var removed = para.removeChild(boldElm);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p id="example2"&gt;&lt;a href="#" onclick="removeBElm(); return false;"&gt;Click this link&lt;/a&gt; to see the above script in action.&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Sep 2006 22:50:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2598</guid>
      <author>bradalyst (brad)</author>
    </item>
  </channel>
</rss>
