Skip to content

Instantly share code, notes, and snippets.

View Victa's full-sized avatar
🏠
Working from home

Victor Coulon Victa

🏠
Working from home
View GitHub Profile
@Victa
Victa / gist:1020464
Created June 11, 2011 11:20
Parse url to get hostname
var a = document.createElement('a');
a.href = url;
// any property of window.location works here:
document.write('The hostname of ' + url + ' is ' + a.hostname);
@Victa
Victa / gist:1020467
Created June 11, 2011 11:21
Create link with Javascript
html="<a href='"+url+"'>"+text+"</a>" // before
html=text.link(url) // after
@Victa
Victa / gist:1024891
Created June 14, 2011 13:28
addSlashes / stripSlashes Javascript
String.prototype.addSlashes = function()
{return this.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');};
String.prototype.stripSlashes = function()
{return this.replace(/\\(.?)/g, function (s, n1){switch (n1){case '\\':return '\\';case '0':return '\u0000';case '':return '';default:return n1;}});};
@Victa
Victa / gist:1062047
Created July 3, 2011 07:55
HTML5 audio stop
HTMLAudioElement.prototype.stop=function(){this.pause();this.currentTime=0};
@Victa
Victa / gist:1069105
Created July 7, 2011 08:40
Javascript MD5
var MD5 = function (string) {
function RotateLeft(lValue, iShiftBits) {
return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
}
function AddUnsigned(lX,lY) {
var lX4,lY4,lX8,lY8,lResult;
lX8 = (lX & 0x80000000);
lY8 = (lY & 0x80000000);
@Victa
Victa / gist:1069110
Created July 7, 2011 08:42
JavaScript Array Contains
/**
* Array.prototype.[method name] allows you to define/overwrite an objects method
* needle is the item you are searching for
* this is a special variable that refers to "this" instance of an Array.
* returns true if needle is in the array, and false otherwise
*/
Array.prototype.contains = function ( needle ) {
for (i in this) {
if (this[i] == needle) return true;
}
@Victa
Victa / gist:1069115
Created July 7, 2011 08:44
Random Hex Color
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var ramdomColor = '#'+Math.round(0xffffff * Math.random()).toString(16);
@Victa
Victa / gist:1071334
Created July 8, 2011 07:48
htmlEntities for JavaScript
function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
@Victa
Victa / lessversion.css
Created July 16, 2011 06:48
CSS3 Spinner
#spinner {
top:50%;
left:50%;
width:32px;
height:0;
margin:-16px 0 0 -16px;
padding-top:32px;
position:absolute;
overflow:hidden;
* {
@Victa
Victa / gist:1086106
Created July 16, 2011 07:29
Testing CSS media queries in JS
if (window.matchMedia('only screen and (max-device-width: 480px)').matches) {
// Asynchronously load iphone.js
} else if (window.matchMedia('only screen and (min-device-width: 481px) and ' +
'(max-device-width: 1024px) and ' +
'(orientation: portrait)').matches) {
// Asynchronously load ipad-portrait.js
}