Skip to content

Instantly share code, notes, and snippets.

Events bubble by default. So the difference between the two is:
target is the element that triggered the event (e.g., the user clicked on)
currentTarget is the element that the event listener is attached to.
source: https://stackoverflow.com/a/10086501/1735394
@townivan
townivan / post-about-flexbox.txt
Last active July 27, 2022 19:48
Return to my post about Flexbox at https://ivan.town/
See link above. Trying a goofy way to attach comments to a generic page.
@townivan
townivan / getCurrentYear.html
Created January 4, 2022 16:51
Display the current year without having to manually update it each time.
<span id="cdate"></span><script>document.getElementById("cdate").innerHTML = (new Date().getFullYear());</script>
@townivan
townivan / a11y-tracking-pixels.js
Last active June 17, 2021 19:01
Those annoying tracking pixels mess up my a11y scan. This is an attempt to add alt, role, and aria attributes to these guys. Uses MutationObserver to immediately update the img tags when they are added to the DOM by the tracking snippet's action.
// early in the head as possible
var observer = new MutationObserver(function(mutations){
for (var i=0; i < mutations.length; i++){
for (var j=0; j < mutations[i].addedNodes.length; j++){
checkNode(mutations[i].addedNodes[j]);
}
}
});
observer.observe(document.documentElement, {
@townivan
townivan / setAttrib.js
Created October 6, 2020 20:18
a function to set an attribute with JavaScript
function sestAttrib(el_id, el_attr, el_value){
if (document.getElementById(el_id)){ // if element exists
document.getElementById(el_id)[el_attr] = el_value;
}
}
using System;
using System.Collections.Generic;
// test at https://dotnetfiddle.net/
public class Program
{
public static void Main()
{
Console.WriteLine("List practice");
@townivan
townivan / setup.js
Last active December 26, 2019 15:39
quick gulp4 project setup
npm i gulp -D
npm i browser-sync -D
npm i gulp-sass -D
// favicon fix for index.html
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
touch gulpfile.js
// This gulpfiles expect a 'dist' and 'src' folder (for easy ftp, less easy for github pages)
@townivan
townivan / ie11-DOM-looping.js
Created November 7, 2019 16:56
ie11 JS work-arounds
// converting nodelist to an array...for ie11:
// ie11 won't like [...nodelist] or Array.from(nodelist) so use:
let elements = Array.prototype.slice.call(document.querySelectorAll('.things'));
// looping through the array....for ie11:
// ie11 won't like elements.map() so use:
elements.forEach(function(el) {
console.log('el:', el);
});
/**
* Be amazed at this use for destructuring
* If you pass args as an obj instead of
* distinct values like f('red','male',23)
* You can use destructuring in the function
* to get what you want without needing to
* figure out which position it should be in!
*/
function f( {name, age} = {}){ // destructure the received {} to name and age.
console.log('age:',age); // age: 23
@townivan
townivan / local-php-on-mac.txt
Created August 22, 2019 11:57
Run php locally on mac
php -S 127.0.0.1:8080