Convert px to em, % and pt #Javascript
// http://jsfiddle.net/jnelsonin/ctfy158o/
$("#pixels").keyup(function(){      		
 var point = ($(this).val() * 3) / 4;
 var yem = point / 12;
 var percent = yem * 100;
      		
 $("#point").text(point+"pt");
 $("#yem").text(yem+"em");
 $("#percent").text(percent+"%");
});
Append url parameters to links with classname #Javascript
//Append url parameters to links with classname
(function() {
var domainsToDecorate = ['domain.com'], queryParams = ['utm_medium','utm_source','utm_campaign','utm_term','utm_content'];
var links = document.querySelectorAll('.rewrite-url'); 
for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {
 for (var domainIndex = 0; domainIndex < domainsToDecorate.length; domainIndex++) { 
   if (links[linkIndex].href.indexOf(domainsToDecorate[domainIndex]) > -1 && links[linkIndex].href.indexOf("#") === -1) {
	 links[linkIndex].href = decorateUrl(links[linkIndex].href);
   }
 }
}
function decorateUrl(urlToDecorate) {
 urlToDecorate = (urlToDecorate.indexOf('?') === -1) ? urlToDecorate + '?' : urlToDecorate + '&';
 var collectedQueryParams = [];
 for (var queryIndex = 0; queryIndex < queryParams.length; queryIndex++) {
   if (getQueryParam(queryParams[queryIndex])) {
	 collectedQueryParams.push(queryParams[queryIndex] + '=' + getQueryParam(queryParams[queryIndex]))
   }
 }
 return urlToDecorate + collectedQueryParams.join('&');
}
function getQueryParam(name) {
 if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(window.location.search))
   return decodeURIComponent(name[1]);
}
Hubspot Form Callbacks #Hubspot
// https://legacydocs.hubspot.com/global-form-events
// onBeforeFormInit - onFormReady - onFormSubmit - onFormSubmitted
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
  // Form Ready
 } else if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
  // Form Submit
 }
});
ipv6 to v4 #Javascript
//FUNC FROM https://jsfiddle.net/asheroto/u7hfjm9k/
function IP6to4(ip6) {
    function parseIp6(ip6str) {
        const str = ip6str.toString();

        // Initialize
        const ar = new Array();
        for (var i = 0; i < 8; i++) ar[i] = 0;

        // Check for trivial IPs
        if (str == '::') return ar;
        
        // Parse
        const sar = str.split(':');
        let slen = sar.length;
        if (slen > 8) slen = 8;
        let j = 0;
        i = 0
        for (i = 0; i < slen; i++) {
            // This is a "::", switch to end-run mode
            if (i && sar[i] == '') {
                j = 9 - slen + i;
                continue;
            }
            ar[j] = parseInt(`0x0${sar[i]}`);
            j++;
        }

        return ar;
    }

    var ip6parsed = parseIp6(ip6);
    const ip4 = `${ip6parsed[6] >> 8}.${ip6parsed[6] & 0xff}.${ip6parsed[7] >> 8}.${ip6parsed[7] & 0xff}`;
    return ip4;
}
Fetch .json file #Javascript
function fetchJSONFile(path, callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                var data = JSON.parse(httpRequest.responseText);
                if (callback) callback(data);
            }
        }
    };
    httpRequest.open('GET', path);
    httpRequest.send(); 
}

fetchJSONFile('file.json', function(data){
	// Do something with Data
    console.log(data);
});
Trim Input Whitespace #Javascript
/* trim-whitespace-input.js*/
$('.no-spaces-field').keyup(function() {
  $(this).val($(this).val().replace(/ +?/g, ''));
});
jQuery Autocomplete form Array #Javascript
$("#country").autocomplete({
    source:[{label:"France",value:"FR"}, {label:"United Kingdom",value:"UK"}],
    minLength: 2,
    select: function(event, ui) {
        event.preventDefault();
        $("#country").val(ui.item.label);
    }
});
Download Textarea Content as Html File #Javascript
/* Download Textarea Content as Html File.js */
$('#download-button').click(function() {
    if ('Blob' in window) {
        var fileName = prompt('Please enter file name to save', 'Untitled.html');
        if (fileName) {
            var textValue = $('textarea').html();
            var textToWrite = htmlDecode(textValue);
            var textFileAsBlob = new Blob([textToWrite], {
                type: 'application/xhtml+xml'
            });
            if ('msSaveOrOpenBlob' in navigator) {
                navigator.msSaveOrOpenBlob(textFileAsBlob, fileName);
            } else {
                var downloadLink = document.createElement('a');
                downloadLink.download = fileName;
                downloadLink.innerHTML = 'Download File';
                if ('webkitURL' in window) {
                    downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
                } else {
                    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
                    downloadLink.click(function() {
                        document.body.removeChild(event.target);
                    });
                    downloadLink.style.display = 'none';
                    document.body.appendChild(downloadLink);
                }
                downloadLink.click();
            }
        }
    } else {
        alert('Your browser does not support the HTML5 Blob.');
    }
});
Pass UTMs to iframe src #Javascript
// Iframe Container
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(function() {
	var oldIframe = jQuery('#iframe');
	if (oldIframe.length) {
		var src = oldIframe.attr('src');
		if (src) {
			var iframeContent = $('<iframe id="iframe-new" src="' + src + location.search + '" style="border: none;width: 100%;background-color:#fff;"></iframe>');
			oldIframe.replaceWith(iframeContent);
		}
	}
});
</script>
<iframe id="iframe" src="https://go.domain.com/form.html"></iframe>
Create iFrame from Textarea Content Html #Javascript
/* Create iFrame from Textarea Content Html */
function setFrame() {
    var HTMLval = TextareaContentID.value;
    var iframe = document.getElementById('iframeID');
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write(HTMLval);
    iframe.contentWindow.document.close();
}
Detect Click Outside of the Element #Javascript
 // Hide Div when Click Outside of the Element using jQuery / https://www.codexworld.com/
$(document).mouseup(function(e){
    var container = $("#elementID");

    // If the target of the click isn't the container
    if(!container.is(e.target) && container.has(e.target).length === 0){
        container.hide();
    }
});
Pass Iframe height to Parent #Javascript
<!-- Iframe Container -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var Iframe = jQuery('#iframe');
window.addEventListener('message', function(e) { 
	var eventName = e.data[0]; 
	var data = e.data[1]; 
	switch(eventName) { 
		case 'setHeight': 
			newIframe.height(data); 
		break;
	} 
}, false);
</script>
<iframe id="iframe" src="https://go.domain.com/form.html"></iframe>
<!-- Iframe Content -->
<script>
var StatusResizing = 'on';
setInterval(function() {
	if(StatusResizing == 'on') {
		resize();
	}
}, 1000);
function resize() {
	var height = $('body').outerHeight();
	window.parent.postMessage(["setHeight", height], "*"); 
}</script>
Detect if iOs #Javascript
/* Detect iOs */
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
// do something
}
Detect Device from screen size #Javascript
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body onload="getDevice()">
	<p id="response" style=""> </p> 
	<script src="https://code.jquery.com/jquery-3.5.0.js"> </script>
	<script>
		function getDevice() {
			var screenWidth = screen.width;
            var responseP = $('#response');
            if(screenWidth <= 480) {
            	responseP.text('mobiles');
            }
            if(screenWidth > 480 && screenWidth <= 1024) {
            	responseP.text('tablets');
            }
            if(screenWidth => 768 && screenWidth <= 1024) {
            	responseP.text('tablets (landscape)');
            }
            if(screenWidth => 1025 && screenWidth <= 1280) {
            	responseP.text('Laptops and Desktops');
            }
            if(screenWidth => 1281) {
            	responseP.text('Large Desktops');
            }
		}
	</script>
</body>
</html> 

No more items to load