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

Benoit Asselin http://www.ab-d.fr

« Newer Snippets
Older Snippets »
Showing 21-30 of 31 total

How to detect a browser iPhone ?

The browser is :


Mozilla/5.0 (iPhone; U; CPU like Mac OS X; fr) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3



Source: ab-d.fr
Internet with iPhone

Some problems with charset in UTF-8 ?

So you can use this request MySQL before all others, for fix your problems :
...
mysql_query( "SET NAMES 'utf8' " );
...


Source: ab-d.fr
Languages: PHP and MySQL

Use get_magic_quotes_gpc();

function f_magic_quotes($text) {
	if ( !get_magic_quotes_gpc() ) {
		return addslashes($text);
	} else {
		return $text;
	}
}


function f_clean_quotes($text) {
	if ( !get_magic_quotes_gpc() ) {
		return $text;
	} else {
		return stripslashes($text);
	}
}


Source: ab-d

Write a variable PHP with spaces or accents

Write a variable PHP with spaces or accents

<?php

${'New variable text'} = 'This is a new variable text.';

echo ${'New variable text'};
// This is a new variable text.

?>


Source: ab-d

in_array() in JavaScript

The same function as in_array() in php.

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}


Sample :
var v_array = [ 5, 10, 15, 20, 25];
document.writeln(v_array.in_array(10));  // true
document.writeln(v_array.in_array(11));  // false


Source: PHP Manual in_array()

Include CSS Stylesheet by DOM


function includeCSS(p_file) {
	var v_css  = document.createElement('link');
	v_css.rel = 'stylesheet'
	v_css.type = 'text/css';
	v_css.href = p_file;
	document.getElementsByTagName('head')[0].appendChild(v_css);
}



Source: Document Object Model (DOM) Level 1 Specification

Safari 3 - Resizable Text Fields

With Safari 3, you can resize the TEXTAREA, but you can control this properties with CSS3.


<textarea cols="30" rows="10" style="resize: both;">www.AB-D.fr presents the new TEXTAREA</textarea>

<textarea cols="30" rows="10" style="resize: horizontal;">www.AB-D.fr presents the new TEXTAREA</textarea>

<textarea cols="30" rows="10" style="resize: vertical;">www.AB-D.fr presents the new TEXTAREA</textarea>

<textarea cols="30" rows="10" style="resize: none;">www.AB-D.fr presents the new TEXTAREA</textarea>



Links:
http://www.apple.com/safari/
http://www.w3.org/TR/css3-ui/#resize

IE doesn't support element.setAttritube('style')

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="fr">
<head>

<title>.setAttribute('style','');</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
<!--

window.onload = function() {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		document.getElementById('test').style.cssText = 'background:gray; color:white;';
	} else {
		/* document.getElementById('test').style.cssText = 'background:gray; color:white;'; */
		document.getElementById('test').setAttribute('style', 'background:gray; color:white;');
	}
}

-->
</script>

</head>

<body>

<div id="test">document.getElementById('test').setAttribute('style', 'background:gray; color:white;')</div>

</body>

</html>

Another way of written "target"

Another way of written "target" and valid in XHTML


<a href="http://www.google.com/" onclick="window.open(this.href); return false;">Link</a>

Save preference key for Widgets ( Dashboard - Mac OSX )

Save preferences
if (window.widget) {
	widget.setPreferenceForKey(value, 'myKey');
}


Load preferences
if (window.widget) {
	if (!(widget.preferenceForKey('myKey') === undefined)) {
		var myKey = widget.preferenceForKey('myKey');
	}
}
« Newer Snippets
Older Snippets »
Showing 21-30 of 31 total