Skip to content

Instantly share code, notes, and snippets.

View adamcjoiner's full-sized avatar

Adam C. Joiner adamcjoiner

View GitHub Profile
@adamcjoiner
adamcjoiner / animate-and-reset.js
Created February 20, 2018 15:53
Add CSS animation to element and then reset it (to animate it again)
$('#btn-animate').click(function() {
// Shake the 'Codes' dropdown
animEl = $('#dropdown-codes');
animEl.addClass('shake animated'); // ~add the anim. CSS class
animEl.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function (e) {
animEl.removeClass('shake animated'); // ~remove it when finished
});
});
@adamcjoiner
adamcjoiner / fullscreen.js
Created October 15, 2017 18:17
Make your web app load in full screen mode when added to iOS Homescreen
/*
Found on StackOverflow:
iPhone Safari Web App opens links in new window
https://stackoverflow.com/questions/2898740/iphone-safari-web-app-opens-links-in-new-window
I have problem with web after adding icon to Home Screen. If the web is launched from Home Screen, all links will open in new window in Safari (and lose full screen functionality). How can I prevent it? I couldn't find any help, only the same unanswered question.
ANSWER:
@adamcjoiner
adamcjoiner / jquery-methods-in-vanilla-js.md
Last active September 28, 2017 03:43
Common Jquery Methods In Raw JS

Common Jquery Methods In Raw JavaScript

Events

// jQuery
$(document).ready(function() {
  // code
})
@adamcjoiner
adamcjoiner / xml-to-json.js
Created September 27, 2017 23:42
Convert XML -> JSON
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
@adamcjoiner
adamcjoiner / dom-to-json.js
Last active September 27, 2017 23:49 — forked from sstur/dom-to-json.js
Convert DOM Node ->JSON string format (and back again)
function toJSON(node) {
node = node || this;
var obj = {
nodeType: node.nodeType
};
if (node.tagName) {
obj.tagName = node.tagName.toLowerCase();
} else
if (node.nodeName) {
obj.nodeName = node.nodeName;
@adamcjoiner
adamcjoiner / dom-to-object
Last active September 28, 2017 03:46 — forked from alain75007/elementToObject
DOM Node -> Object -> JSON
function elementToObject(element, o) {
var el = $(element);
var o = {
tagName: el.tagName
};
var i = 0;
for (i ; i < el.attributes.length; i++) {
o[el.attributes[i].name] = el.attributes[i].value;
}
@adamcjoiner
adamcjoiner / inject-remote-script.js
Last active March 11, 2023 18:53
Inject remote Javascript into page asynchronously without using vanilla JS (no jQuery)
// Method used in Google Analytics:
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function(){
// remote script has loaded
};
script.src = 'http://www.google-analytics.com/ga.js';
@adamcjoiner
adamcjoiner / debug_to_console.php
Created July 26, 2017 06:58
Send PHP debug message to Javascript console
<?php
/**
* Output debug message to browser console
*/
function debug_to_console($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
@adamcjoiner
adamcjoiner / js_objects.js
Created July 25, 2017 14:14
An example of how to use an object stored in a variable that contains multiple methods
var winston = require('winston');
winston.add(
winston.transports.File, {
filename: 'node-log-file.json.log',
level: 'info',
json: true,
eol: '\n', // for Windows, or `eol: '\n',` for *NIX OSs
timestamp: true
}
);
@adamcjoiner
adamcjoiner / injector.js
Created July 19, 2017 21:50
Inject remote Javascript file into currently loaded page via browser's location bar
javascript: javascript: (function() {
var s = 'https://nytimes.github.io/svg-crowbar/svg-crowbar.js';
var e = document.createElement('script');
e.setAttribute('src', s);
e.setAttribute('class', 'svg-crowbar');
document.body.appendChild(e);
})();