Skip to content

Instantly share code, notes, and snippets.

View raspo's full-sized avatar
🔥
Deleting code

Tommaso Ferrari raspo

🔥
Deleting code
View GitHub Profile
@raspo
raspo / gist:2016244
Created March 11, 2012 12:22
Create a copy of an object with jQuery
var newObject = jQuery.extend(true, {}, oldObject);
@raspo
raspo / gist:2016252
Created March 11, 2012 12:26
Quick and dirty test css3 support via javascript
var supports = (function() {
var div = document.createElement('div'),
vendors = 'Khtml Ms O Moz Webkit'.split(' '),
len = vendors.length;
return function(prop) {
if ( prop in div.style ) return true;
prop = prop.replace(/^[a-z]/, function(val) {
return val.toUpperCase();
@raspo
raspo / gist:2016259
Created March 11, 2012 12:28
Get querystring parameter by name using javascript
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
@raspo
raspo / gist:2016262
Created March 11, 2012 12:31
Convert string value to unicode via javascript
function toUnicode(theString) {
var unicodeString = '';
for (var i=0; i < theString.length; i++) {
var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
while (theUnicode.length < 4) {
theUnicode = '0' + theUnicode;
}
theUnicode = '\\u' + theUnicode;
unicodeString += theUnicode;
}
@raspo
raspo / gist:2016265
Created March 11, 2012 12:32
Stripslahes function for javascript
function stripslashes( str ) {
return (str + '').replace(/\\(.?)/g, function (s, n1){
switch (n1) {
case '\\':
return '\\';
case '0':
return '\u0000';
case '':
return '';
default:
@raspo
raspo / gist:2016270
Created March 11, 2012 12:32
Addslashes function for javascript
function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
//str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
@raspo
raspo / gist:2016273
Created March 11, 2012 12:34
Get numbers from a string (simple regex)
'asd123'.match(/\d+$/) // 123
'asd123asd'.match(/\d+/) // 123
@raspo
raspo / gist:2507325
Created April 27, 2012 08:10
Strip HTML code from string
var myString = '<span title="title">test</span>';
myString.replace(/<(?:.|\n)*?>/gm, '');
@raspo
raspo / gist:2551988
Created April 29, 2012 17:09
Shorten a string and optionally truncate at the last word boundary
function truncateString( string, num, useWordBoundary ){
var isLong = string.length > num,
newString = string.replace( /(^\s)|(\s$)/gi, '' ),
isOneWord = newString.match(/\s/gi) === null;
newString = isLong ? newString.substr(0,num-1) : newString;
newString = ( useWordBoundary && isLong && !isOneWord ) ? newString.substr(0,newString.lastIndexOf(' ')) : newString;
return isLong ? newString +' ...' : newString;
};
@raspo
raspo / gist:3009858
Created June 28, 2012 08:17
Split string only once
function splitOnce( str, sep ){
var components = str.split( sep ),
result = [];
result[0] = components.shift();
result[1] = components.join( sep );
return result;
}
// Example
var string = 'key=value&key2=value2&key3=value3';