Never been to DZone Snippets 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

About this user

Patrick http://www.hunlock.com

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Broadcasting DZONE Shares

// This snippet will allow you to show your DZONE shares
// on your web page. Visit your shares page, click on the "feed" link
// to get the URL of your personal shares feed then paste it in the first line
// in the script.

var sharedURL='';  // Place your url between the quotes.

function getFeed(url, callback) {
   var newScript = document.createElement('script');
       newScript.type = 'text/javascript';
       newScript.src = 'http://pipes.yahoo.com/pipes/9oyONQzA2xGOkM4FqGIyXQ/run?&_render=JSON&_callback='+callback+'&feed=' + sharedURL;
   document.getElementsByTagName("head")[0].appendChild(newScript);
}

function dzone(feed) {
   var tmp='';
   for (var i=0; i<feed.value.items.length; i++) {
      tmp+='<a href="'+feed.value.items[i].link+'" rel="nofollow">';
      tmp+=feed.value.items[i].title+'</a><br>';
   }
   document.getElementById('dzoneLinks').innerHTML=tmp;
}

getFeed(sharedURL, 'dzone');


// Paste that code at the end of your page. In your HTML place the following two divisions
// where you would like your feed to appear. (feel free to style the divisions however you wish)
// you can add to/subtract from the outer division but the inner division will always be overwritten
// with your shares.

<div id='dzoneLayer'>
My DZONE Recommendations
   <div id='dzoneLinks'>
   </div>
</div>

Automatically Create a Page Index

// description of your code here

First create your HTML. You can style it however you want but the division names must remain the same.

<div id='indexDiv'>
   <div id='indexContents'></div>
</div>


Next place the following code at the end of your content.
els=document.getElementsByTagName('h4');
idx='';
for (i=0; i<els.length; i++) {
  idx += '<a href="#quickIDX'+i+'">'+els[i].innerHTML+'</a><BR>';
  els[i].innerHTML='<a name="quickIDX'+i+'"></a>'+els[i].innerHTML;
}
if (!els.length) {
  document.getElementById('indexDiv').style.display='none';
} else {
  document.getElementById('indexContents').innerHTML=idx;
}


Any H4 tag on the page will now be indexed and a link to the element placed in the index the script built. Place the index anywhere on your page and style it however you wish.

Synchronous AJAX

// Snippet showing how to use synchronous AJAX

function getFile(url) {
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
     AJAX.open("GET", url, false);                             
     AJAX.send(null);
     return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}

var fileFromServer = getFile('http://somedomain.com/somefile.txt');

Javascript Ajax Object

// Just a stub function we'll tell ajaxObject to call when it's done
// callback functions get responseText, and responseStat respectively
// in their arguments.
function fin(responseTxt,responseStat) {
  alert(responseStat+' - '+responseTxt);
}

// create a new ajaxObject, give it a url it will be calling and
// tell it to call the function "fin" when its got data back from the server.
var test1 = new ajaxObject('http://someurl.com/server.cgi',fin);
    test1.update();
		
// create a new ajaxObject, give it a url and tell it to call fin when it
// gets data back from the server.  When we initiate the ajax call we'll
// be passing 'id=user4379' to the server.		
var test2 = new ajaxObject('http://someurl.com/program.php',fin);
    test2.update('id=user4379');
		
// create a new ajaxObject but we'll overwrite the callback function inside
// the object to more tightly bind the object with the response hanlder.
var test3 = new ajaxObject('http://someurl.com/prog.py', fin);
    test3.callback = function (responseTxt, responseStat) {
      // we'll do something to process the data here.
      document.getElementById('someDiv').innerHTML=responseTxt;
    }
    test3.update('coolData=47&userId=user49&log=true');	
		
// create a new ajaxObject and pass the data to the server (in update) as
// a POST method instead of a GET method.
var test4 = new ajaxObject('http://someurl.com/postit.cgi', fin);
    test4.update('coolData=47&userId=user49&log=true','POST');	


function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

Array Search Prototype

This prototype extends the Array object to allow for searches
within the Array. It will return false if nothing is found. If
item(s) are found you'll get an array of indexes back which matched
your search request. It accepts strings, numbers, and regular expressions as search
criteria. 35 is different than '35' and vice-versa.
// Examples
var test=[1,58,'blue','baby','boy','cat',35,'35',18,18,104]
result1=test.find(35);    //returns 6
result2=test.find(/^b/i); //returns 2,3,4
result3=test.find('35');  //returns 7
result4=test.find(18);    // returns 8,9
result5=test.find('zebra'); //returns false


Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS