Advertisement
Virajsinh

My JS Functions

Nov 23rd, 2021 (edited)
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.73 KB | None | 0 0
  1. // Title Case Function Ex. String.toProperCase();
  2. String.prototype.toProperCase = function () {
  3.     return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  4. };
  5.  
  6. // dd/mm/yyyy format
  7. function getCurrentDate(){
  8.     var date = new Date();
  9.     var d = date.getDate(); // Date
  10.     var m = date.getMonth() + 1; // Month
  11.     var y = date.getFullYear(); // Year
  12.     var current_date = d+'/'+m+'/'+y ;
  13.     return current_date;
  14. }
  15.  
  16. function date_format(dateObject) {
  17.     // dd/mm/yyyy format date return
  18.     var d = new Date(dateObject);
  19.     var day = d.getDate();
  20.     var month = d.getMonth() + 1;
  21.     var year = d.getFullYear();
  22.     if (day < 10) {
  23.         day = "0" + day;
  24.     }
  25.     if (month < 10) {
  26.         month = "0" + month;
  27.     }
  28.     var date = day + "-" + month + "-" + year;
  29.     return date;
  30. }
  31.  
  32. /**
  33. * Sets a Cookie with the given name and value.
  34. *
  35. * name       Name of the cookie
  36. * value      Value of the cookie
  37. * [expires]  Expiration date of the cookie (default: end of current session)
  38. * [path]     Path where the cookie is valid (default: path of calling document)
  39. * [domain]   Domain where the cookie is valid
  40. *              (default: domain of calling document)
  41. * [secure]   Boolean value indicating if the cookie transmission requires a
  42. *              secure transmission
  43. */                        
  44. function setCookie(name, value, expires, path, domain, secure) {
  45.     document.cookie = name + "=" + escape(value) +
  46.         ((expires) ? "; expires=" + expires.toGMTString() : "") +
  47.         ((path) ? "; path=" + path : "") +
  48.         ((domain) ? "; domain=" + domain : "") +
  49.         ((secure) ? "; secure" : "");
  50. }
  51.  
  52. /**
  53. * Gets the value of the specified cookie.
  54. *
  55. * name  Name of the desired cookie.
  56. *
  57. * Returns a string containing value of specified cookie,
  58. *   or null if cookie does not exist.
  59. */
  60.  
  61. function getCookie(name) {
  62.     var dc = document.cookie;
  63.     var prefix = name + "=";
  64.     var begin = dc.indexOf("; " + prefix);
  65.     if (begin == -1) {
  66.         begin = dc.indexOf(prefix);
  67.         if (begin != 0) return null;
  68.     } else {
  69.         begin += 2;
  70.     }
  71.     var end = document.cookie.indexOf(";", begin);
  72.     if (end == -1) {
  73.         end = dc.length;
  74.     }
  75.     return unescape(dc.substring(begin + prefix.length, end));
  76. }
  77.  
  78. /**
  79. * Deletes the specified cookie.
  80. *
  81. * name      name of the cookie
  82. * [path]    path of the cookie (must be same as path used to create cookie)
  83. * [domain]  domain of the cookie (must be same as domain used to create cookie)
  84. */
  85.  
  86. function deleteCookie(name, path, domain) {
  87.     if (getCookie(name)) {
  88.         document.cookie = name + "=" +
  89.             ((path) ? "; path=" + path : "") +
  90.             ((domain) ? "; domain=" + domain : "") +
  91.             "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  92.     }
  93. }
  94.  
  95. // No Right Click
  96. function f1() {
  97.     if (document.all) {
  98.         return false;
  99.     }
  100. }
  101.  
  102. function f2(e) {
  103.     if (document.layers || (document.getElementById & !document.all)) {
  104.         if (e.which == 2 || e.which == 3) {
  105.             return false;
  106.         }
  107.     }
  108. }
  109. if (document.layers) {
  110.     document.captureEvents(Event.MOUSEDOWN);
  111.     document.onmousedown = f1;
  112. } else {
  113.     document.onmouseup = f2;
  114.     document.oncontextmenu = f1;
  115. }
  116. document.oncontextmenu = new function ("return false");
  117.  
  118. // Generate Random String With Number
  119. function generateRandomAlphaNum(len) {
  120.     var rdmString = "";
  121.     for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
  122.     return rdmString.substr(0, len);
  123.  
  124. }
  125.  
  126. function getExtension(filename) {
  127.   return filename.split(".").pop();
  128. }
  129.  
  130. // https://sebhastian.com/javascript-filename-extension/
  131. function formatFileSize(bytes,decimalPoint) {
  132.    if(bytes == 0) return '0 Bytes';
  133.    var k = 1000,
  134.        dm = decimalPoint || 2,
  135.        sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
  136.        i = Math.floor(Math.log(bytes) / Math.log(k));
  137.    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  138. }
  139.  
  140. // https://www.knowledgewalls.com/johnpeter/books/one-day-one-thing-to-know/how-to-redirect-with-getpost-data-in-jquery-custom-method
  141. function url_redirect(options){
  142.   var $form = $("<form />");
  143.  
  144.   $form.attr("action",options.url);
  145.   $form.attr("method",options.method);
  146.  
  147.   for (var data in options.data)
  148.   $form.append('<input type="hidden" name="'+data+'" value="'+options.data[data]+'" />');
  149.  
  150.   $("body").append($form);
  151.   $form.submit();
  152. }
  153.  
  154. $(function(){
  155.   /*jquery statements */
  156.   url_redirect({url: "http://www.knowledgewalls.com",
  157.     method: "get",
  158.     data: {"q":"KnowledgeWalls"}
  159.    });
  160. });
  161.  
  162. //https://bitexperts.com/Question/Detail/3316/determine-file-size-in-javascript-without-downloading-a-file
  163. function getFileSize(url)
  164. {
  165.     var fileSize = '';
  166.     var http = new XMLHttpRequest();
  167.     http.open('HEAD', url, false); // false = Synchronous
  168.     http.send(null); // it will stop here until this http request is complete
  169.     // when we are here, we already have a response, b/c we used Synchronous XHR
  170.     if (http.status === 200) {
  171.         fileSize = http.getResponseHeader('content-length');
  172.         console.log('fileSize = ' + fileSize);
  173.     }
  174.     return fileSize;
  175. }
  176.  
  177. $(function () {
  178.     $(document).ajaxStart(function () {
  179.         console.log('===  app.js [4723] === Start Ajax');
  180.         loader_show();
  181.     });
  182.  
  183.     $(document).ajaxStop(function () {
  184.         console.log('===  app.js [4723] === Stop ajax');
  185.         loader_hide();    
  186.     });
  187.  
  188.     $(document).ajaxError(function () {
  189.         console.log('===  app.js [4723] === Error ajax');
  190.         loader_hide();
  191.     });
  192. });
  193.  
  194. // LocalData Set
  195. function set_userdata(key, array) {
  196.     if(key !='' && array !='' ){
  197.         localStorage.setItem(key,JSON.stringify(array));
  198.     }
  199. }
  200.  
  201. // LocalData Get
  202. function get_userdata(array_key) {
  203.     if(array_key != ''){
  204.         return JSON.parse(localStorage.getItem(array_key));
  205.     }
  206. }
  207. // Check inArray Item
  208. function inArray(array_item, check_array_list) {
  209.     if($.inArray(array_item, check_array_list) !== -1){
  210.         return true;
  211.     }else{
  212.         return false;
  213.     }
  214. }
  215.  
  216. // Remove Last URI Segment From URL
  217. function remove_last_uri(url_path=''){
  218.     if( url_path !='' )
  219.         return url_path.substring(0, url_path.lastIndexOf('/'));
  220. }
  221. //https://github.com/kelvintaywl/dentist.js/blob/master/js/dentist.js
  222. Get Data From URL
  223.  
  224. // Multi Dimensional Array to Get Single Array Column Row Comma Separate Array
  225. function array_column(array, column_name){
  226.     return array.map(x => x[column_name]);
  227. }
  228.  
  229. const convertTime12to24 = (time12h) => {
  230.     const [time, modifier] = time12h.split(' ');
  231.     let [hours, minutes] = time.split(':');
  232.     if (hours === '12') {
  233.         hours = '00';
  234.     }
  235.     if (modifier === 'PM') {
  236.         hours = parseInt(hours, 10) + 12;
  237.     }
  238.     return `${hours}:${minutes}`;
  239. }
  240.  
  241. function validate_reset_form(form_id=''){
  242.     if(form_name !=""){
  243.         $("#"+form_id).validate().resetForm(); 
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement