Skip to content

Instantly share code, notes, and snippets.

View ridhotegar's full-sized avatar

Ridho Tegar Pambudi ridhotegar

View GitHub Profile
@ridhotegar
ridhotegar / index.html
Created January 19, 2023 23:16
Handle broken image or link with JavaScript
<script>
document.addEventListener("DOMContentLoaded", function() {
let images = document.querySelectorAll("img");
images.forEach(img => {
img.addEventListener("error", function () {
console.log("Broken image: ", img);
img.src = `https://fakeimg.pl/${img.width}x${img.height}/`;
});
let xhr = new XMLHttpRequest();
@ridhotegar
ridhotegar / Main.kt
Last active July 27, 2022 08:06
Kotlin Filter ArrayList<String>() | https://pl.kotl.in/zkn5GcMwM
fun main() {
val aaa = ArrayList<String>()
aaa.add("Mobil")
aaa.add("Motor")
var bbb = ArrayList<String>()
for(row in aaa) {
if(row.lowercase().contains("mot")) {
bbb.add(row)
}
}
@ridhotegar
ridhotegar / MainActivity.kt
Created June 4, 2022 15:50
Kotlin View Binding Example Project
package com.example.viewbindingexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.viewbindingexample.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
@ridhotegar
ridhotegar / index.js
Last active December 10, 2023 02:57
Download file with XMLHttpRequest js
function save(blob) {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'zip_10MB.zip'; //filename
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert('your file has downloaded!');
@ridhotegar
ridhotegar / Create_Folder_Cordova_iOS.js
Last active December 23, 2021 05:00
Create folder directory cordova iOS
function createFolder (folder_name, success) {
document.addEventListener('deviceready', () => {
let path = cordova.file.documentsDirectory+"NoCloud/"
window.resolveLocalFileSystemURL(path, e => {
e.getDirectory(`${folder_name}`, { create: true, exclusive: false }, success, error => console.log(error));
}, error => console.log(error))
})
}
function checkFolder (folder_name, found, notFound) {
document.addEventListener('deviceready', () => {
let path = cordova.file.documentsDirectory+"NoCloud/"
window.resolveLocalFileSystemURL(path, e => {
e.getDirectory(`${folder_name}`, {create: false}, found, notFound);
}, error => console.log(error))
})
}
@ridhotegar
ridhotegar / Promise_localStorage.js
Last active December 23, 2021 05:01
Wait all localStorage done with promise
const localSet = (key, value) => {
    return new Promise(resolve => {
        resolve(localStorage.setItem(key, value))
    })
}
Promise.all([
localSet('KeyA', 'ValueA'),
localSet('KeyB', 'ValueB'),
localSet('KeyC', 'ValueC')
@ridhotegar
ridhotegar / JQuery_File_Download.js
Last active December 23, 2021 05:02
XMLHttpRequest download with JQuery
$.ajax({
url: "https://file-examples-com.github.io/uploads/2017/02/zip_10MB.zip",
cache: false,
xhr: function(){
var xhr = new XMLHttpRequest();
xhr.responseType= 'blob';
xhr.onprogress = e => console.log(`${parseInt((e.loaded/e.total)*100)}%`)
return xhr;
},
success: e => console.log('done')
@ridhotegar
ridhotegar / Disable_All_Onclick_JQuery.js
Last active December 23, 2021 05:03
Disable all onclick on page html with jquery
let clickAct = []
const _do = {
disable: () => {
let el = $('[onclick]'), count = el.length
el.each((i, e) => {
$(e).addClass(`click_${i}`)
clickAct[i] = $(e).attr('onclick')
if(!--count) { el.removeAttr("onclick") }
})
},
@ridhotegar
ridhotegar / JQuery_Prototype_Property.js
Last active December 23, 2021 05:05
Sample constructor function Data table JQuery
$(document).ready(function() {
$.fn.setDt = function(){
this.dataTable({
"paging" : true,
"ordering" : false,
"info" : true,
"searching" : true
})
}
});