Skip to content

Instantly share code, notes, and snippets.

View dhunmoon's full-sized avatar

Atul Chaudhary dhunmoon

View GitHub Profile
@dhunmoon
dhunmoon / addEventHandler.js
Created July 21, 2023 11:47
Adding event to an element (backward compatable)
/**
* @param {Object} el Element where the event needed to be added
* @param {string} type Type of event which is getting added
* @param {function} handler Event handler which will get called.
*/
function addHandler(el, type, handler) {
//overwrite the existing function
if (el.addEventListener){ //DOM2 Events
addHandler = function(el, type, handler) {
el.addEventListener(type, handler, false)
@dhunmoon
dhunmoon / getDocumentPageHeight.js
Created August 16, 2022 09:26
Get document height
/**
* Document sizes are a browser compatibility nightmare because, although all browsers expose clientHeight and s
* crollHeight properties, they don't all agree how the values are calculated.
* @return {float} Height of the page after calulation.
* */
function getDocumentHeight(){
var body = document.body,
html = document.documentElement;
@dhunmoon
dhunmoon / arrayDepth.js
Created April 30, 2018 08:41
Get Depth of an array.
function depthOf(arr) {
if(!Array.isArray(arr))
throw "Not an Array"
var depth = 1;
var i;
for(var i = 0; i< arr.length; i++){
if (!Array.isArray(arr[i])) continue;
if(Array.isArray(arr[i])){
var depth = depthOf(arr[i]) + 1;
@dhunmoon
dhunmoon / jsJsonGrepFilter.js
Created March 23, 2017 11:01
JS:JSON Grep Filter
$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {
return n.website==='google';
});
@dhunmoon
dhunmoon / jsJsonDataFilter.js
Created March 23, 2017 11:00
JS:JSON Data Filter
$([
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
])
.filter(function (i,n){
return n.website==='google';
});
@dhunmoon
dhunmoon / jsDatatableFilter.js
Created March 23, 2017 10:58
JS:Datatable Multiple Filters
/* Custom filtering function which will search data in column four between two values */
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var min = parseInt( $('#min').val(), 10 );
var max = parseInt( $('#max').val(), 10 );
var age = parseFloat( data[3] ) || 0; // use data for the age column
if ( ( isNaN( min ) && isNaN( max ) ) ||
( isNaN( min ) && age <= max ) ||
( min <= age && isNaN( max ) ) ||
@dhunmoon
dhunmoon / jsCSVFileCreation.js
Created March 22, 2017 09:58
JS:SCV File Creation
var data = [["name1", "city1", "some other info"], ["name2", "city2", "more info"]];
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function(infoArray, index){
dataString = infoArray.join(",");
csvContent += index < data.length ? dataString+ "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
@dhunmoon
dhunmoon / jsCSVFileCreationBlob.js
Created March 22, 2017 09:57
JS: CSV File Creation Using BLOB
function exportToCsv(filename, rows) {
var processRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
@dhunmoon
dhunmoon / idDetection.js
Created March 11, 2017 23:36
JS: IE Detection
/*Default string of IE 10:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Default string of IE 11:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Default string of IE 12 (aka Edge):
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0
Default string of Edge 13 (thx @DrCord):
@dhunmoon
dhunmoon / jsGetOuterHTML.js
Last active January 18, 2018 16:31
JS:Get Outer HTML
$('<div>').append($('#ID').clone()).html();