Skip to content

Instantly share code, notes, and snippets.

View danielt69's full-sized avatar
:octocat:
Focusing

Daniel danielt69

:octocat:
Focusing
View GitHub Profile
function getScript(url, callback, timeout) {
var timeoutIdx,
s = document.createElement('script');
s.type = 'text/' + (url.type || 'javascript');
s.src = url.src || url;
s.async = false;
s.onreadystatechange = s.onload = function() {
// this code handles two scenarios, whether called by onload or onreadystatechange
@danielt69
danielt69 / makeAsyncRequest.js
Created August 5, 2018 09:00
Vanilla js ajax request
function makeAsyncRequest(
url,
onComplete,
timeout,
postData,
requestHeaders
) {
var xhr = new (window.XMLHttpRequest || window.ActiveXObject)(
'MSXML2.XMLHTTP.3.0'
);
@danielt69
danielt69 / arrayDiff.js
Created July 31, 2018 07:34
Difference Between 2 JavaScript Arrays
Array.prototype.diff = function (a) {
return this.filter(function (i) {
return a.indexOf(i) === -1;
});
};
// usage:
[1, 2, 3, 4, 5, 6].diff([2, 4, 6]);
// => [1, 3, 5]
var util = {
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
escapeRegExp: function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},
hasClass: function(element, selector) {
var s = ' ';
return (
element.nodeType === 1 &&
@danielt69
danielt69 / copy to clipboard.js
Last active August 18, 2020 15:12
copy to clipboard simple JS function
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
$(document).ready( function() {
var x=0; setInterval(function() {
var dots = ""; x++; for (var y=0; y < x%3; y++) {dots+=".";} $("#loading-dots").text(dots);
} , 500);
});
@danielt69
danielt69 / $.fn.isBottomInScreen.js
Last active June 4, 2018 16:25
check to see if element is scrolled out of the screen and act on it
$.fn.isBottomInScreen = function (outFn, inFn) {
outFn = outFn || $.noop();
inFn = inFn || $.noop();
var $self = $(this);
var inFlag = true;
$(window).on('scroll', function () {
requestAnimationFrame(function () {
if (($self.offset().top + $self.height()) < $(window).scrollTop()) { // element is out of screen
if (inFlag) {
$self.addClass('out').removeClass('in').trigger('out');
@danielt69
danielt69 / document.ready.js
Last active January 9, 2019 09:28
document.ready my version of JQuery's $(document).ready()
document.ready = function(f){
f = f || function(){};
if (document.readyState == "interactive" || "complete") {
f();
} else {
document.addEventListener("DOMContentLoaded", function(e) {
f();
});
}
}
@danielt69
danielt69 / gradient-text.css
Created June 3, 2018 14:35
gradient text style
element.style {
background-image: linear-gradient(to left, #777dff 0%, #f154ff 50%, #f0357c 100%),linear-gradient(to right, #777dff 0%, #497ce2 50%, #37bdde 100%);
color: transparent;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
@danielt69
danielt69 / clearConsole.js
Created April 26, 2018 08:50
clearConsole.js - Avoid `console` errors in browsers that lack a console.
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeline', 'timelineEnd', 'timeStamp', 'trace', 'warn'
];