DZone 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

Snippets

  • submit to reddit

Recent Snippets

                    
/********************************************************
   Function: getChildNodeName

     note:
       Cycle through the child nodes and find the first instance of the name.

*********************************************************/
XMLNode.prototype.getChildNodeName = function(nName) {
   for (var i=0; i<this.childNodes.length; i++) {
      var tChild = this.childNodes[i];
      if (tChild.nodeName === nName) {
         return tChild;
      }  
   }
   return null;
};
                
                    Serve XHTML as application/xhtml+xml to browsers which accept it (and to W3C Validator).

<?php 
header("Vary: Accept");
if (stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') || stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator"))
{
  header('Content-type: application/xhtml+xml');
  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}
else
{
  header('Content-type: text/html');
}
?>
                
                    A request for this came up on the CF-Talk list recently. Here's what I came up with.
<!--- The list with want to clean up --->
<cfset lst = "a,list,with,a,few,duplicates">
<!--- Use a struct to build a set of the list elements --->
<cfset set = StructNew()>
<cfloop index="elem" list="#lst#">
    <cfset set[elem] = "">
</cfloop>
<!--- Convert the set back to a list --->
<cfset lst = StructKeyList(set)>
This sort of this should be familiar to all you perlmongers out there.                
                    
<%
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("pragma","no-cache");
%>
                
                    
function loadXMLDoc(url) {
		
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
    
        req = new XMLHttpRequest();
        //function_name is the function to process request
        req.onreadystatechange = function_name;
        req.open("GET", url, true);
        req.send(null);
        
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {

            //function_name is the function to process request        
            req.onreadystatechange = actualizarLista;
            req.open("GET", url, true);
            req.send();
        }
    }
}
                
                    
if (req.readyState == 4) {

	if (req.status == 200) {
		
            // ...processing statements go here...

            response = req.responseXML;
            
            results = response.getElementsByTagName(name);

	}
}
                
                    in Firefox

user_pref("javascript.options.strict", true);
console display 'warning'                
                    Assume you have copied a file across the network using something like this:

scp host:/path/to/file .

Then assume something goes wrong, so the transfer is interrupted. To continue the transfer, do this:

rsync --partial host:/path/to/file .

(Or, rather, always use rsync as above when transferring very large files.)

(Also note: I don't know how frequently rsync flushes its buffer, but don't assume something is wrong just because the file you're downloading doesn't increase in size in the file system. When you interrupt rsync using Ctrl-C, it will flush its buffers.)                
                    

begin
   Thread.current.kill
   exit(1)
   #`kill -9 #{$$}` # this will successfully kill you
   puts "after kill"
ensure
   puts "HA HA!"
end


always interesting to play with this kind of code.
and discover how much ensure means ENSURE !!!!

Ruby: Great Language.

                
                    
class DefaultController < ApplicationController
    def default
        result = Net::HTTP.get_response(
            "realserver.internal.net",
            @request.env["REQUEST_URI"]
            )
        #render error if result. ...
        render_text result.body
        end
    end