Skip to content

Instantly share code, notes, and snippets.

View jeandcr's full-sized avatar
👋

Jean-Daniel Cramer jeandcr

👋
View GitHub Profile
@jeandcr
jeandcr / react-18-use-effect-once.ts
Last active July 26, 2022 09:29
React 18 - Execute on mount, only once. codebase from TanStack Table v8 talk.
import React from "react";
const doSomething = () => console.log('doing something');
/**
useEffect(() => {
doSomething();
return () => cleanup();
}, [whenThisChanges]);
@jeandcr
jeandcr / loop-find.js
Created May 13, 2019 10:45
JS Loops using map, filter, reduce and find
//
// Finding a single element in the array
//
// filter returns an array with less items than the original array
const performSomething = (item) => {
return item
}
const items = ['a', 'b', 'c']
items.forEach((item) => {
performSomething(item)
@jeandcr
jeandcr / gtm_ga_pageview_insert.js
Created April 25, 2019 14:49
A way to listen to pageview events from the ga js object through GTM
if(window.ga) {
window.ga(function (tracker) {
// Grab a reference to the default sendHitTask function.
var originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function (model) {
originalSendHitTask(model);
@jeandcr
jeandcr / change-favicon.js
Created March 15, 2019 13:59 — forked from mathiasbynens/change-favicon.js
Dynamically changing favicons with JavaScript
/*!
* Dynamically changing favicons with JavaScript
* Works in all A-grade browsers except Safari and Internet Explorer
* Demo: http://mathiasbynens.be/demo/dynamic-favicons
*/
// HTML5™, baby! http://mathiasbynens.be/notes/document-head
document.head || (document.head = document.getElementsByTagName('head')[0]);
function changeFavicon(src) {
@jeandcr
jeandcr / queryString_from_object.js
Created March 1, 2019 11:02
Build query parameters from object
/**
* ------------------------------------
* Using Map & Join
* ------------------------------------
*/
// ES6
var queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');
// ES5
@jeandcr
jeandcr / queryString_from_form.js
Created March 1, 2019 10:56
Build query parameters from form
function form_params( form )
{
var params = new Array()
var length = form.elements.length
for( var i = 0; i < length; i++ )
{
element = form.elements[i]
if(element.tagName == 'TEXTAREA' )
{
@jeandcr
jeandcr / ads.js
Created November 19, 2018 13:29
Adblocker test
var e=document.createElement('div');
e.id='I3q96EY1VjNB';
e.style.display='none';
document.body.appendChild(e);
@jeandcr
jeandcr / query_string.js
Created November 16, 2018 16:09
Return a URL query string by key.
/**
*
* Returns a URL query parameter by it's key.
* eg. You have a URL: http://host.com/some/path?uniquekey=value
* The function call qs('uniquekey') returns 'value'
*
* @param key string
* @return string
*
*/
@jeandcr
jeandcr / ClientID_GTM_var.js
Last active November 9, 2018 11:25
Client ID in GTM JS Variable
/**
* This snippet is meant to return your
* analytics client ID, of your web visitor
* to a variable of your choosing.
*
* This REQUIRES that you have Classic/Universal Analytics.
*
* Step-by-step:
* 1. Go to your GTM container. (https://tagmanager.google.com)
* 2. Go to Variables.
@jeandcr
jeandcr / arrayToURL.js
Created October 12, 2018 08:58
JS Array to URLEncoded
/*
* Extend Array prototype
*/
Array.prototype.toQueryString = function(){
var out = new Array();
for(key in this){
out.push(key + '=' + encodeURIComponent(this[key]));
}