Category Archives: jQuery

jQuery Get File Extension Before Uploading File

A jQuery example of how to get file size before uploading: $(‘input[type=file]’).change(function(e){   var ext = e.target.files[0].name.replace(/^.*\./, ”).toLowerCase();   alert("File Extension: " + ext);   … });$(‘input[type=file]’).change(function(e){ var ext = e.target.files[0].name.replace(/^.*\./, ”).toLowerCase(); alert("File Extension: " + ext); … });

Simple jQuery File Upload SimpleUpload.js

Today I found this awesome jQuery file upload plugin: http://simpleupload.michaelcbrook.com/ The official simpleUpload.js jQuery plugin page on Github is this: https://github.com/michaelcbrook/simpleUpload.js The plugin is hosted by Content Delivery Network (CDN) JsDelivr: https://www.jsdelivr.com/package/npm/jquery-simple-upload Below there is an example to use the jQuery plugin showing a progress bar. The JavaScript section looks like this: $(document).ready(function(){   $(‘input[type=file]’).change(function(){ […]

Repeat AJAX GET Until a Specific Response is Met in jQuery

Here is how to repeat an AJAX GET call until a specific response is met: function repeated_ajax_check(url) { $.ajax ({ type: ‘GET’, url: url, dataType: ‘json’, success: function(result) { if(result.success) { console.log("We finally met our response!"); console.log(result); //Do something other here, for example: //$(‘.result-message’).html(‘RESPONSE MET!’); //$(‘.result-message’).show(); }else{ console.log("Response not yet met."); console.log(result); wait(1000); repeated_ajax_check(url); } […]

Calculate Elapsed Time of a Function in jQuery

Here is how to calculate elapsed time of a function in jQuery: function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; return h+":"+(m < 10 ? ‘0’+m : m)+":"+(s < 10 ? ‘0’+s : s); }   var seconds = 1;   $(‘#elapsed-time’).html(‘Elapsed time: ‘ + secondsTimeSpanToHMS(seconds)).show();   […]

Convert Seconds to H:M:S in jQuery

Simple function to convert seconds in H:M:S in jQuery: function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; return h+":"+(m < 10 ? ‘0’+m : m)+":"+(s < 10 ? ‘0’+s : s); }function secondsTimeSpanToHMS(s){ var h = Math.floor(s/3600); s -= h*3600; var m = Math.floor(s/60); s -= m*60; […]

Truncate a String by Custom Length in jQuery

Simple function to truncate a string in jQuery: function truncateString(input, max){ if (input.length > max) return input.substring(0, max) + ‘…’; else return input; };function truncateString(input, max){ if (input.length > max) return input.substring(0, max) + ‘…’; else return input; }; Here is an example usage: $(‘#filename’).html("File: " + truncateString(file.name, 50));$(‘#filename’).html("File: " + truncateString(file.name, 50));

Convert POST Fields to Objects in jQuery

Here is a simple function to convert POST fields to object: function postArray(form){ var data = {}; form = $(form).serializeArray(); for(var i in form) data[form[i].name] = form[i].value; return data; }function postArray(form){ var data = {}; form = $(form).serializeArray(); for(var i in form) data[form[i].name] = form[i].value; return data; } You can use it like this: data: […]

How to Sleep/Wait in jQuery (Synchronous)

Here are two sample functions that will wait, causing interruption: function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() – start) > milliseconds){ break; } } }   function wait(ms) { const start = performance.now(); while(performance.now() – start < ms); }function sleep(milliseconds) { […]