[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Немного вкусных плюшех
RCuPeR
Хочу поделится небольшим набором функций, которые я использую в работе практически каждый день. Было бы прекрасно если бы кто-то смог дополнить этот список.

И так, поехали:

Свернутый текст
function setCookie(name, value, expires) 
{
var today = new Date();

today.setTime( today.getTime() );

if (expires)
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = getParam('sJsCookiePrefix') + name + "=" +escape( value ) +
( (
expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( (
getParam('sJsCookiePath') ) ? ";path=" + getParam('sJsCookiePath') : "" ) +
( (
getParam('sJsCookieDomain') ) ? ";domain=" + getParam('sJsCookieDomain') : "" );

debug('Adding Cookie: ' + name + ' -> ' + value);
}

function deleteCookie(name)
{
if (this.getCookie(name))
{
document.cookie = getParam('sJsCookiePrefix') + name + "=" +
( (
getParam('sJsCookiePath') ) ? ";path=" + getParam('sJsCookiePath') : "") +
( (
getParam('sJsCookieDomain') ) ? ";domain=" + getParam('sJsCookieDomain') : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
debug('Deleting Cookie: ' + name);
}
}


function getCookie(check_name)
{
var a_all_cookies = document.cookie.split( ';' );
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false;
var check_name = getParam('sJsCookiePrefix') + check_name;

for ( i = 0; i < a_all_cookies.length; i++ )
{
a_temp_cookie = a_all_cookies[i].split( '=' );

cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

if ( cookie_name == check_name )
{
b_cookie_found = true;

if ( a_temp_cookie.length > 1 )
{
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
}
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name = '';
}

if ( !b_cookie_found )
{
return null;
}
}


function parse(strInputCode)
{
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1)
{
return (p1 == "lt")? "<" : ">";
});
return strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
}

/**
* PHP
*/


function substr(sString, iStart, iLength)
{
if(iStart < 0)
{
iStart += sString.length;
}

if(iLength == undefined)
{
iLength = sString.length;
}
else if(iLength < 0)
{
iLength += sString.length;
}
else
{
iLength += iStart;
}

if(iLength < iStart)
{
iLength = iStart;
}

return sString.substring(iStart, iLength);
}

function str_repeat(str, repeat)
{
var output = '';
for (var i = 0; i < repeat; i++)
{
output += str;
}
return output;
}

function print_r( array, return_val )
{
var output = "", pad_char = " ", pad_val = 4;

var formatArray = function (obj, cur_depth, pad_val, pad_char)
{
if (cur_depth > 0)
{
cur_depth++;
}

var base_pad = repeat_char(pad_val*cur_depth, pad_char);
var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
var str = "";

if (obj instanceof Array || obj instanceof Object) {
str += "Array\n" + base_pad + "(\n";
for (var key in obj) {
if (obj[key] instanceof Array) {
str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
} else {
str += thick_pad + "["+key+"] => " + obj[key] + "\n";
}
}

str += base_pad + ")\n";
} else if(obj == null || obj == undefined) {
str = '';
} else {
str = obj.toString();
}

return str;
};

var repeat_char = function (len, pad_char) {
var str = "";
for(var i=0; i < len; i++) {
str += pad_char;
};
return str;
};
output = formatArray(array, 0, pad_val, pad_char);

if (return_val !== true) {
document.write("<pre>" + output + "</pre>");
return true;
} else {
return output;
}
}


function isset()
{
var a=arguments; var l=a.length; var i=0;

if (l==0) {
throw new Error('Empty isset');
}

while (i!=l) {
if (typeof(a[i])=='undefined' || a[i]===null) {
return false;
} else {
i++;
}
}

return true;
}

function empty(mixed_var) {
var key;

if (mixed_var === ""
|| mixed_var === 0
|| mixed_var === "0"
|| mixed_var === null
|| mixed_var === false
|| mixed_var === undefined
|| trim(mixed_var) == ""
){
return true;
}
if (typeof mixed_var == 'object') {
for (key in mixed_var) {
if (typeof mixed_var[key] !== 'function' ) {
return false;
}
}

return true;
}
return false;
}

function trim(str, charlist) {
var whitespace, l = 0, i = 0;
str += '';

if (!charlist) {
whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
} else {
charlist += '';
whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
}

l = str.length;
for (i = 0; i < l; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}


l = str.length;
for (i = l - 1; i >= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}


return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function ltrim ( str, charlist ) {
charlist = !charlist ? ' \s\xA0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
var re = new RegExp('^[' + charlist + ']+', 'g');
return (str+'').replace(re, '');
}

function rtrim ( str, charlist ) {
charlist = !charlist ? ' \s\xA0' : (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
var re = new RegExp('[' + charlist + ']+$', 'g');
return (str+'').replace(re, '');
}

function function_exists( function_name ) {
if (typeof function_name == 'string'){
return (typeof window[function_name] == 'function');
} else{
return (function_name instanceof Function);
}
}


function explode( delimiter, string, limit ) {
var emptyArray = { 0: '' };

if ( arguments.length < 2
|| typeof arguments[0] == 'undefined'
|| typeof arguments[1] == 'undefined' )
{
return null;
}

if ( delimiter === ''
|| delimiter === false
|| delimiter === null )
{
return false;
}

if ( typeof delimiter == 'function'
|| typeof delimiter == 'object'
|| typeof string == 'function'
|| typeof string == 'object' )
{
return emptyArray;
}

if ( delimiter === true ) {
delimiter = '1';
}

if (!limit) {
return string.toString().split(delimiter.toString());
} else {
var splitted = string.toString().split(delimiter.toString());
var partA = splitted.splice(0, limit - 1);
var partB = splitted.join(delimiter.toString());
partA.push(partB);
return partA;
}
}


function in_array(needle, haystack, strict) {
var found = false, key, strict = !!strict;

for (key in haystack) {
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
found = true;
break;
}
}


return found;
}

function htmlspecialchars (string, quote_style, charset, double_encode) {
// http://kevin.vanzonneveld.net
// + original by: Mirek Slugen
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Nathan
// + bugfixed by: Arno
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Ratheous
// + input by: Mailfaker (http://www.weedem.fr/)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: felix
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// % note 1: charset argument not supported
// * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
// * returns 1: '<a href='test'>Test</a>'
// * example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
// * returns 2: 'ab"c'd'
// * example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
// * returns 3: 'my "&entity;" is still here'


var optTemp = 0, i = 0, noquotes= false;
if (typeof quote_style === 'undefined' || quote_style === null) {
quote_style = 2;
}
string = string.toString();
if (double_encode !== false) { // Put this first to avoid double-encoding
string = string.replace(/&/g, '&');
}
string = string.replace(/</g, '<').replace(/>/g, '>');

var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE' : 1,
'ENT_HTML_QUOTE_DOUBLE' : 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE' : 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
quote_style = [].concat(quote_style);
for (i=0; i < quote_style.length; i++) {
// Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
}
else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}

quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/'/g, ''');
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}

return string;
}




Спустя 3 часа, 57 минут, 56 секунд (18.04.2012 - 13:20) Nikitian написал(а):


_____________
Гнусный социопат с комплексом Бога.
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.