Skip to content

Instantly share code, notes, and snippets.

View tobiasroeder's full-sized avatar
:octocat:
I <3 </>

Tobias Röder tobiasroeder

:octocat:
I <3 </>
View GitHub Profile
@tobiasroeder
tobiasroeder / readJSON.js
Created June 5, 2019 14:36
Pur JavaScript to read a JSON file for the YouTube, GitHub or other API
fetch(YOUR_JSON_FILE_URL).then(function(responsive) {
return responsive.json();
}).then(function(obj) {
console.log(obj);
});
@tobiasroeder
tobiasroeder / data.css
Created June 22, 2019 08:22
CSS before/after content property with the content from an attribute.
[data-before]::before {
content: attr(data-before);
}
[data-after]::after {
content: attr(data-after);
}
@tobiasroeder
tobiasroeder / splitter.js
Created July 13, 2019 14:22
A function which splitter a number.
// 1234 => 1.234
function splitter(t) {
if (t = '' + t,
t.length > 3) {
var e = t.length % 3,
n = e > 0 ? t.substring(0, e) : '';
for (i = 0; i < Math.floor(t.length / 3); i++) n += 0 == e && 0 == i ? t.substring(e + 3 * i, e + 3 * i + 3) : '.' + t.substring(e + 3 * i, e + 3 * i + 3);
return n;
}
@tobiasroeder
tobiasroeder / loadJSON.js
Created July 13, 2019 14:23
A function which load the content of a json file.
// load JSON
function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
} else {
// callback([{}]);
@tobiasroeder
tobiasroeder / fade.js
Last active October 10, 2019 20:27
Simple fade in/out function without any library. (pur JavaScript)
// source: https://chrisbuttery.com/articles/fade-in-fade-out-with-javascript/
// fadeOut (fade.js)
function fadeOut(el) {
el.style.opacity = 1;
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val -= .1) < 0)) {
el.style.opacity = val;
requestAnimationFrame(fade);
@tobiasroeder
tobiasroeder / mixArray.js
Last active September 27, 2023 09:52
A way to mix an array in JavaScript.
// For production use i recommend: https://phuoc.ng/collection/1-loc/shuffle-an-array/
// function
function mixArray(array) {
let newArray = [],
maxRandNbr = array.length;
for (let i = 0; i = maxRandNbr; i++) {
randNbr = Math.round(Math.random() * (maxRandNbr - 1));
@tobiasroeder
tobiasroeder / hasCssProperty.js
Created May 15, 2021 13:35
Check if an element contains a specific css property.
// returns a boolean if the element contains that property
const hasCssProperty = (elmt, property) => {
const elmtPropertyValue = window.getComputedStyle(elmt, null).getPropertyValue(property);
return elmtPropertyValue ? true : false;
}
// live example
const elmt = document.querySelector('#video');
if (hasCssProperty(elmt, 'aspect-ratio')) {
@tobiasroeder
tobiasroeder / .htaccess
Last active January 15, 2023 08:49
Very simple PHP Router
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
@tobiasroeder
tobiasroeder / vigenere.js
Created September 24, 2021 09:15
Vigenère-Chiffre
/*
* source: https://rosettacode.org/wiki/Vigen%C3%A8re_cipher#JavaScript
*/
// vigenere
function vigenere(text, key, decode) {
// helper
const ordA = a => a.charCodeAt(0) - 65;
// main
let i = 0, b;
@tobiasroeder
tobiasroeder / chr.php
Created October 18, 2021 14:02
0-9 A-Z a-z
<?php
$chars = '';
// 65 - 122 (A-Za-z)
// 48 - 122 (0-9A-Za-z)
for ($i = 48; $i <= 122; $i++) {
// skip 58 - 64 and 91 - 96
if ($i >= 58 && $i <= 64 || $i >= 91 && $i <= 96)
continue;