Skip to content

Instantly share code, notes, and snippets.

View janbiasi's full-sized avatar
🌐
Just building

Jan R. Biasi janbiasi

🌐
Just building
View GitHub Profile
@janbiasi
janbiasi / jquery-micro.js
Last active August 29, 2015 14:03
Micro jQuery method for easy DOM selection without plugins on 11 lines
var $ = function(selector) {
var matches = {
'#': 'getElementById', // $('#myId')
'.': 'getElementsByClassName', // $('.myClass')
'@': 'getElementsByName', // $('@myName')
'=': 'getElementsByTagName', // $('=body')
'?': 'querySelectorAll' // $('?anything')
}, rex = /[=#@.*]/.exec(selector)[0];
var nodes = document[matches[rex]](selector.split(rex)[1]);
return nodes.length > 1 ? nodes : nodes[0];
@janbiasi
janbiasi / classes.js
Created July 1, 2014 07:20
Working with HTML classes like jQuery's addClass and removeClass
function isElement(obj) {
try {
// For Firefox, Opera and Chrome
return obj instanceof HTMLElement;
}
catch(e){
// IE Bugfix
return (typeof obj==="object") &&
(obj.nodeType===1) && (typeof obj.style === "object") &&
(typeof obj.ownerDocument ==="object");
@janbiasi
janbiasi / timestamp.js
Last active August 29, 2015 14:03
Creating timestamps in JavaScript
/**
* Generate a datestamp in your format
* @return {string} datestamp
*/
function datestamp(seperator) {
var today = new Date(), dd = today.getDate(), mm = today.getMonth() + 1, yyyy = today.getFullYear();
seperator = seperator != null && seperator.length > 0 ? seperator : "/";
dd = dd < 10 ? '0' + dd : dd;
mm = mm < 10 ? '0' + mm : mm;
return dd + seperator + mm + seperator + yyyy;
@janbiasi
janbiasi / targeting.js
Last active August 29, 2015 14:03
Add target _blank to all links
// All links pure JS solution
var anchors = document.getElementsByTagName('a');
for (var i=0; i < anchors.length; i++) {
anchors[i].setAttribute('target', '_blank');
}
// All links w. jQuery
$('a').attr('target', '_blank');
// All outgoing links w. jQuery
@janbiasi
janbiasi / excerpt.cs
Last active August 29, 2015 14:03
String excerption in JavaScript and C# for shortening messages or other strings
/// <summary>
/// Create excerpt string with n characters
/// </summary>
/// <param name="longString">The long string</param>
/// <param name="delimiter">How many chars there should be</param>
/// <returns>Shortened string</returns>
public static string Excerpt(string longString, int delimiter = 30) {
string shortened = ""; int i = 0;
if (String.IsNullOrEmpty(longString) == false) {
if (longString.Length > delimiter) {
@janbiasi
janbiasi / export.php
Created September 9, 2014 07:52
A simple Wordpress post exporter written in PHP
<?php
// Security checking
define('ABSPATH') or die ("Script kiddies!");
// Defining path and filename
$outPath = 'postbackup/';
$fileName = 'wp_posts_' . date('m-d-Y-His A e') . '.xml';
// Indlude the wordpress stuff
require(dirname(dirname(__FILE__)) . '/wp-load.php');
@janbiasi
janbiasi / helper.js
Last active August 29, 2015 14:07
Helper
(function(window, undefined) {
var defineSingleEventListener = function(element, event, callback) {
try {
if(element.attachEvent) {
return element.attachEvent('on' + event, callback);
} else {
return element.addEventListener(event, callback, false);
}
} catch(e) {
@janbiasi
janbiasi / base.css
Created November 3, 2014 08:21
Basic CSS for WebApps
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,700,600);
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
html,
body,
@janbiasi
janbiasi / database-helper.php
Created November 11, 2014 11:54
A simple PHP helper for working JSON including a short demo
<?php
// Get content from the DB
function readDatabase($path) {
return (array)json_decode(file_get_contents($path));
}
// Save content to the DB
function saveDatabase($path, $content) {
file_put_contents($path, json_encode($content));
@janbiasi
janbiasi / apache.sh
Last active August 29, 2015 14:09
A tiny installer for linux including Apache, MySQL, PHP and GitLab (v5.0)
#!/bin/sh
echo "You have to be the root user to execute this script!"
echo "What is your OS?"
echo "1 OpenSuSE"
echo "2 Ubuntu"
echo "3 RedHat"
echo "4 Debian"
echo "5 Go Back"
echo "6 Quit"