Tag Archive for simple

jsCookies – my simple easy pure js javascript cookies function

//  create my jsCookies function
var jsCookies = {

	// this gets a cookie and returns the cookies value, if no cookies it returns blank ""
	get: function(c_name) {
		if (document.cookie.length > 0) {
			var c_start = document.cookie.indexOf(c_name + "=");
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				var c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) {
					c_end = document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	},

	// this sets a cookie with your given ("cookie name", "cookie value", "good for x days")
	set: function(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toUTCString());
	},

	// this checks to see if a cookie exists, then returns true or false
	check: function(c_name) {
		c_name = jsCookies.get(c_name);
		if (c_name != null && c_name != "") {
			return true;
		} else {
			return false;
		}
	}

};
// end my jsCookies function

// USAGE - get    ::   jsCookies.get("cookie_name_here");  [returns the value of the cookie]
// USAGE - set    ::   jsCookies.set("cookie_name", "cookie_value", 5 );  [give name, val and # of days til expiration]
// USAGE - check  ::   jsCookies.check("cookie_name_here");  [returns only true or false if the cookie exists or not]

source

Simplest jQuery Slideshow

$(function(){
    $('.fadein img:gt(0)').hide();
    setInterval(function(){
      $('.fadein :first-child').fadeOut()
         .next('img').fadeIn()
         .end().appendTo('.fadein');}, 
      3000);
});

// HTML Markup
<div class="fadein">
  <img src="<a href="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg">" >http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg"></a>
  <img src="<a href="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg">" >http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg"></a>
  <img src="<a href="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg">" >http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg"></a>
</div>

// CSS
.fadein { position:relative; width:500px; height:332px; }
.fadein img { position:absolute; left:0; top:0; }

source

Simple MySql class

// Author: Dealis
// Site: <a href="http://www.dealis.pl">www.dealis.pl</a>
// Email: <a href="mailto:piotrek290@gmail.com">piotrek290@gmail.com</a>


class MySql {
	public function query($sql){
		$query = @mysql_query($sql, $this->handle) or die(mysql_errno().": ".mysql_error());
		return new Result($query);
	}	
	
	public function connect($host, $root, $pass, $data){
		$connect = @mysql_connect($host, $root, $pass) or die(mysql_errno().": ".mysql_error());
		@mysql_select_db($data, $connect) or die(mysql_errno().": ".mysql_error());
		$this->handle = $connect;
		return $connect;
	}
	
	public function escape($str){
		if(get_magic_quotes_runtime()){
			$str = htmlspecialchars(stripslashes($str));
		}
		
		return @mysql_real_escape_string($str);
	}
	
	public function last(){
		return @mysql_insert_id($this->handle);
	}
	
	public function close(){
		return @mysql_close($this->handle);
	}
}

class Result extends MySql{
	public function Result($result){
		$this->query = $result;
	}
	
	public function fetch(){
		return @mysql_fetch_array($this->query);
	}
	
	public function is(){
		return ($this->query?true:false);
	}
	
	public function first_cell(){
		$row = @mysql_fetch_row($this->query);
		return $row[0];
	}
	
	public function num(){
		return @mysql_num_rows($this->query);
	}
	
	public function finish(){
		return $this->query;
	}
}

source

detect browser to choose CSS file

function queBrowser() {
	$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  
	if (strpos($user_agent, 'Opera') !== false) {$browser = 'opera';  
	} elseif (strpos($user_agent, 'Chrome') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'Firefox/2') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'Firefox/3') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'Firefox') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'Shiretoko') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'Safari') !== false) {$browser = 'estilos';  
	} elseif (strpos($user_agent, 'MSIE 6') !== false) {$browser = 'estilos-ie6';  
	} elseif (strpos($user_agent, 'MSIE 7') !== false) {$browser = 'estilos-ie7'; 
	} elseif (strpos($user_agent, 'MSIE 8') !== false) {$browser = 'estilos-ie8'; 
	} elseif (strpos($user_agent, 'MSIE') !== false) {$browser = 'estilos-ie'; 
	} else {$browser = 'Otro';  
	}
	return $browser;
}

source

SimpleXML Set Attributes

function set_simplexml_attributes($element, $attributes) {
foreach($attributes as $key => $value) {
$element->addAttribute($key, $value);
}
}

// ex:
set_simplexml_attributes($categoryElement, array(
"id" => "1",
"enabled" => "true",
"iconUrl" => $this->thinger,
"tooltip" => "popups/study.swf"
));

source

Simple jQuery Drop Down Menu

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#dropdownbutton").click(function () {
$("#menu").slideToggle("slow");
});
});
</script>

</head>
<body>
<a href="#" id="dropdownbutton">Show Menu</a>
<ul id="menu" style="display:none">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>

source

super over simplified cache control and expire header htaccess

ExpiresActive On
ExpiresDefault A6048000
FileETag none
Header unset Etag
Header unset Last-Modified
Header set Cache-Control "public"

<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi|html?)$">
Header set Cache-Control "private, must-revalidate, proxy-revalidate"
ExpiresDefault A0
ExpiresActive Off
</FilesMatch>

source

Simple Attractors

Peter de Jong:
x' = sin(a * y) - cos(b * x)
y' = sin(c * x) - cos(d * y)

Cliff Pickover:
x' = sin(a * y) + c * cos(a * x)
y' = sin(b * x) + d * cos(b * y)

source

Simple Singleton Template

package {
public class Example {
private static var instance:Example;

public function Example(access:Access){
if (access != null) {
if (Example.instance == null){
Example.instance = this;
}
} else {
throw new Error(”Illegal operation”);
}
}

//provides access to singleton instance
public static function getInstance():Example{
if (instance == null){
instance = new Example();
}
return Example;
}
}
}

source

C# Call a Constructor Overload from the Default Constructor

public class State
{
public State() :this(5)
{
}

public State(int numberOfNodes)
{
//create nodes
}
}

source