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

Yansky http://forboden.com

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

Basic XMLHttprequest

// Basic XMLHttprequest (code from http://www.sitepoint.com/article/take-command-ajax)

function makeHttpRequest(url){

   var http_request = false;

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
   
       http_request = new XMLHttpRequest();
	   
       if (http_request.overrideMimeType){
	   
           http_request.overrideMimeType('text/xml');
		   
       }
	   
   } 
   else if (window.ActiveXObject) { // IE
   
       try{
	   
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
		   
       } 
	   catch(e){
	   
           try{
		   
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
			   
           } 
		   catch (e) {}
       }
	   
   }

   if (!http_request) {
   
       alert('Unfortunatelly you browser doesn\'t support this feature.');
	   
       return false;
	   
   }
   http_request.onreadystatechange = function() {
   
       if (http_request.readyState == 4){
	   
           if (http_request.status == 200){

				alert(http_request.responseText);

           } 
		   else{
		   
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
			   
           }
		   
       }
	   
   };
   
   http_request.open('GET', url, true);
   http_request.send(null);
}

Simple JSON formatting example

// Simple JSON formatting example

({

	'google' : {
	
		'action': 'http://www.google.com.au/search',
		
		'method': 'POST',		
		
		'input': [
		
			'<input type="hidden" value="en" name="hl"/>',
			
			'<input value="" title="Google Search" size="55" name="q" maxlength="2048"/>',
			
			'<input type="hidden" value="en" name="hl"/>'
		
		],
		
		'buttons': [
		
			'<input type="submit" value="Google Search" name="btnG"/>',
			
			'<input type="submit" value="I\'m Feeling Lucky" name="btnI"/>'
		
		],
		
		'image': 'http://www.google.com/intl/en_ALL/images/logo.gif'
		
	},
	
	'yahoo' : {
	
		'action': 'http://search.yahoo.com/search',
		
		'method': 'POST',		
		
		'input': [
		
			'<input value="" maxlength="255" name="q" id="search" type="text">'
		
		],
		
		'buttons': [
		
			'<input value="Search" type="submit">'
		
		],
		
		'image': 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif'		
		
	},	

	'wikipedia' : {
	
		'action': 'http://en.wikipedia.org/wiki/Special:Search',
		
		'method': 'POST',
		
		'input': [
		
			'<input value="" name="search" type="text">'
		
		],
		
		'buttons': [
		
			'<input value="Search" name="go" type="submit">'
		
		],
		
		'image': 'http://en.wikipedia.org/images/wiki-en.png'		
		
	},	

	'imdb' : {
	
		'action': 'http://imdb.com/find',
		
		'method': 'POST',
		
		'input': [
		
			'<input value="" name="q">'
		
		],
		
		'buttons': [
		
			'<input value="Search" type="submit">'
		
		],
		
		'image': 'http://i.media-imdb.com/images/nb15/logo2.gif'		
		
	},	

	'ebay' : {
	
		'action': 'http://search.ebay.com.au/search/search.dll',
		
		'method': 'GET',
		
		'input': [
		
			'<input value="R40" name="from" type="hidden">',
			
			'<input name="satitle" maxlength="300" type="text">'
		
		],
		
		'buttons': [
		
			'<input value="Search" type="submit">'
		
		],
		
		'image': 'http://pics.ebaystatic.com/aw/pics/logos/logoEbay_x45.gif'		
		
	},		
	
	'dictionary' : {
	
		'action': 'http://dictionary.com/search',
		
		'method': 'GET',
		
		'input': [
		
			'<input maxlength="256" value="" name="q" type="text">'
		
		],
		
		'buttons': [
		
			'<input value="Search" type="submit">'
		
		],
		
		'image': 'http://cache.lexico.com/g/d/dictionary_logo.gif'		
		
	},	

	'iboogie' : {
	
		'action': 'http://www.iboogie.com/searchtree.asp',
		
		'method': 'GET',
		
		'input': [
		
			'<input type="text" value="test" name="name_query" />'
		
		],
		
		'buttons': [
		
			'<input type="submit" value=" Search " class="button"/>'
		
		],
		
		'image': 'http://www.iboogie.com/images/iboogie_us.jpg'		
		
	},		
	
})


Some simple javascript xpath examples

// Some simple javascript xpath examples

var canCElC = document.evaluate( '//a[@class="canc"]' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for (var m = 0; m < canCElC.snapshotLength; m++){

	var im = canCElC.snapshotItem(m);

}

var mems = document.evaluate( '//a[contains(@href, "profile")][ not( @class = "skyblue" )]' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for (var l = 0; l < mems.snapshotLength; l++){

	var cThis = mems.snapshotItem(l);

}	

var canHazPics = document.evaluate( '//a[@title= "Click for large image"]' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

document.evaluate( 'html/body/div/div[7]/table/tbody/tr[2]/td[ not( contains(@id, "main") )]' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for(...

//bravo's xpath function shortcut
// if you don't have $x already
function $x(p, c) {
	var i, r = [], x = document.evaluate(p, c || document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
	while(i=x.iterateNext()) r.push(i);
	return r;
}
//
// a function to change history days in links
//
// Usage: ChangeDays(n); where n is 1, 3, 7, 14, 30 or 60 - not sure what other values may do to poor Simones Site
//
function ChangeDays(d) {
	$x('//a[contains(@href, "/forum-user.cfm?id=")][not(contains(@href, "days="))]').forEach(function(e) {
		e.setAttribute('href', e.getAttribute('href').replace(/cfm\?id=/, 'cfm?days='+d+'&id='));
	});
}


// more bravo stuff

// getById
function $i(id) {
	return document.getElementById(id);
}
// xpath unordered nodes
function $xu(p, c) {
	var i, r = [], x = document.evaluate(p, c || document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
	while(i=x.iterateNext()) r.push(i);
	return r;
}
// xpath ordered nodes
function $xo(p, c) {
	var i, r = [], x = document.evaluate(p, c || document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
	while(i=x.iterateNext()) r.push(i);
	return r;
}
// xpath single first node
function $xf(p, c) {
	return document.evaluate(p, c || document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
// xpath single any node
function $xa(p, c) {
	return document.evaluate(p, c || document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
}
// getByCLASS(className, orderedFlag);
// untested!!
function $c(cls, o) {
	var fn=$xu;
	if(o) fn=$xo;
	return fn('//*[@class = "'+cls+'"' +
				' or contains(@class, " '+cls+' ")' +
				' or starts-with(@class, "' +cls+' ")' +
				' or substring(@class,string-length(@class)-'+cls.length+')=" '+cls+'"]');
}
// create Element
function $ec(type, attributes){
	var node = document.createElement(type);
	for (var attr in attributes) if (attributes.hasOwnProperty(attr)){
		node.setAttribute(attr, attributes[attr]);
	}
	return node;
}
// delete Element
function $ed(element) {
	element.parentNode.removeChild(element);
}
// insert element after
function $ea(newNode, node) {
	return node.parentNode.insertBefore(newNode, node.nextSibling);
}
// insert element before
function $eb(newNode, node) {
	return node.parentNode.insertBefore(newNode, node);
}
// make element first child of par
function $ef(newNode, par) {
	return par.insertBefore(newNode, par.firstChild);
}
// make element last child of par
function $el(newNode, par) {
	return par.appendChild(newNode);
}


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