Archive

Archive for the ‘JavaScript’ Category

Javascript get a nice uri from string


This function will transform a string into a nice uri.

You can use this to get a good level seo uri for all your links


function getNiceURI(_dirtyUri)
{
_dirtyUri = _dirtyUri.toLowerCase();
if(_dirtyUri === "") return _dirtyUri;

var encodedURI = encodeURIComponent(_dirtyUri);
var niceURI = encodedURI.replace(/[%20]/g,”_”);
niceURI = niceURI.replace(/[^a-zA-Z0-9-_]/g, ‘_’);

//Elimino eventuali underscore dal primo carattere
while(niceURI.indexOf(“_”) === 0) {
niceURI = _dirtyUri.substring(1, niceURI.length);
}

//Deleting underscores from beginning and end string
while(niceURI.lastIndexOf(“_”) === niceURI.length – 1) {
niceURI = niceURI.substring(0, niceURI.length – 1);
}

//Take off repeated underscores
while(niceURI.indexOf(“__”) !== -1) {
niceURI = niceURI.replace(“__”, “_”);
}

return niceURI;
}

Advertisements

Simple date convertion with javascript


Following there are few functions in javascript that allow you to forget problems and dismiss boring codes to convert
a date with mysql format coming from some ajax calls using javascript:

PS: to use them you must use also the empty() function derived directly from php:

/** 
* Return the day value from a mysql date/datetime 
*/
function getDay(_mySqlDate) {

    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[2].substring(0, 2);
}

/** 
* Return the month value from a mysql date/datetime 
*/
function getMonth(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[1].substring(0, 2);
}

/** 
* Return the year value from a mysql date/datetime 
*/
function getYear(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[0].substring(0, 4);
}

/** 
* Return the hours value from a mysql date/datetime 
*/
function getHours(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        var _arrTimes = _els[1].split(":");
        return _arrTimes[0];
    } else {
        return "";
    }
}

/** 
* Return the minutes value from a mysql date/datetime 
*/
function getMinutes(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        var _arrTimes = _els[1].split(":");
        return _arrTimes[1];
    } else {
        return "";
    }
}

/** 
* Return the seconds value from a mysql date/datetime
 */
function getSeconds(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        _arrTimes = _els[1].split(":");
        return _arrTimes[2];
    } else {
        return "";
    }
}

/** 
* Return the date with into version dd/mm/yyyy or dd/mm/yyyy hh:mm:ss 
*/
function getDateTime(_mySqlDate, _printTime, _noMilliseconds) {

    if(_printTime !== false) _printTime = true;
    if(_noMilliseconds !== true) _noMilliseconds = false;

    var _date = getDay(_mySqlDate)+"/"+getMonth(_mySqlDate)+"/"+getYear(_mySqlDate);

    if (_mySqlDate.indexOf(":") >= 0) {

        var _els = _mySqlDate.split(" ");
        if (getDay(_mySqlDate) == "") {
            return false;
        }

        if (_els[1] == "00:00:00") {
            _els[1] = "";
        }

        if (_printTime) {
            if (_noMilliseconds) {
                _times = _els[1].split(":");
                _date += " " + _times[0] + ":" + _times[1];
            } else {
                _date += " " + _els[1];
            }
        }

        return (_date == "00/00/0000") ? "" : _date;
    } else {

        if (getDay(_mySqlDate) == "") {
            return false;
        }

        return (_date == "00/00/0000") ? "" : _date;
    }
}
Categories: JavaScript

Empty function from php to javascript


One of the most used function of php is the empty() function.
Unfortunately it’s not present in javascript and here there is the derived code.
I hope it will help you:

 

function empty (mixed_var) {
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || 
        mixed_var === null || mixed_var === false || 
        typeof mixed_var === 'undefined') {
        return true;
    }

    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }

    return false;
}

 

Categories: JavaScript

Javascript right click disabled


Here a simple function to disable the right click and also the key of contextmenu from javascript:

<script type='text/javascript'>
 var isNS = (navigator.appName == "Netscape") ? 1 : 0;
  if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
  function mischandler(){
   return false;
 }
  function mousehandler(e){
 	var myevent = (isNS) ? e : event;
 	var eventbutton = (isNS) ? myevent.which : myevent.button;
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
 document.oncontextmenu = mischandler;
 document.onmousedown = mousehandler;
 document.onmouseup = mousehandler;
</script>
Categories: JavaScript

urldecode() e url encode() in javascript



function urlDecode(str){
    str=str.replace(new RegExp('\\+','g'),' ');
    return unescape(str);
}
function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

Categories: JavaScript

explode() in javascript




function explode (delimiter, string, limit) {

    var emptyArray = {0: ''};

    // third argument is not required
    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 {
        // support for limit argument
        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;
    }
}

Categories: JavaScript

implode() in javascript





function implode (glue, pieces) {

    var i = '', retVal='', tGlue='';
    if (arguments.length === 1) {
        pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (pieces instanceof Array) {
            return pieces.join(glue);
        }
        else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];
                tGlue = glue;
            }
            return retVal;
        }
    }
    else {
        return pieces;
    }
}
Categories: JavaScript
%d bloggers like this: