Skip to content

Instantly share code, notes, and snippets.

@A35G
A35G / loadContent.js
Created December 4, 2024 16:00
jQuery's .load() in pure JavaScript - Version using XMLHttpRequest
/**
* Loads an HTML document from a URL (or a portion of it via the “selector” parameter) within a “target” element.
* If present, executes upon completion, a callback.
*
* Example usage:
* loadContent('#target', 'https://www.mypage.tld');
* loadContent('#target', 'https://www.mypage.tld', '#selector');
* loadContent('#target', 'https://www.mypage.tld', '#selector', () => console.log('Hi!'));
* loadContent('#target', 'https://www.mypage.tld', '#selector', myCallback);
* loadContent('#target', 'https://www.mypage.tld', '', myCallback);
@A35G
A35G / loadContent.ts
Created December 4, 2024 15:58
jQuery's .load() in TypeScript
const checkURL = (url: string): boolean => {
try {
return Boolean(new URL(url));
} catch(e) {
return false;
}
}
const isEmpty = (args: any): boolean => (args === "" || args === 0 || args === "0" || args === null || args === false || args === undefined || (Array.isArray(args) && args.length === 0));
@A35G
A35G / loadContent.js
Last active December 4, 2024 16:01
jQuery's .load() in pure JavaScript - Version using fetch
/**
* Loads an HTML document from a URL (or a portion of it via the “selector” parameter) within a “target” element.
* If present, executes upon completion, a callback.
*
* Example usage:
* loadContent('#target', 'https://www.mypage.tld');
* loadContent('#target', 'https://www.mypage.tld', '#selector');
* loadContent('#target', 'https://www.mypage.tld', '#selector', () => console.log('Hi!'));
* loadContent('#target', 'https://www.mypage.tld', '#selector', myCallback);
* loadContent('#target', 'https://www.mypage.tld', '', myCallback);
@A35G
A35G / highlightText.php
Created November 14, 2024 10:22
Searching and highlighting one or more words in a text - Example with string/text predefined
<?php
function highlightText(string $str, array $keyw = [], bool $accurate = false)
{
if (empty($keyw)) return false;
$sText = implode('|', $keyw);
$match = ($accurate) ? '@\b(' . $sText . ')\b@si' : '@(' . $sText . ')@si';
@A35G
A35G / Password-strength-meter.html
Created May 9, 2024 22:18
A very simple password strength tester based on regular expression in Vanilla Javascript.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Password Strength Meter</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
@A35G
A35G / check_push_token.php
Created September 21, 2017 20:26
Is your device's Token for Push Notifications valid yet? Check it out now!
<?php
class PushMessage {
private $url = 'https://fcm.googleapis.com/fcm/send';
private $serverApiKey = "";
private $devices = array();
function __construct($apiKeyIn){
$this->serverApiKey = $apiKeyIn;
@A35G
A35G / easter_egg.js
Created January 21, 2016 23:51
Pieces of code useful for sketching an easter egg in a website or a web app. By entering a word or a sequence of characters / symbols / or other, it is possible to perform a function if the condition is true; In case of 5 second pause between typing and the other, the sequence is emptied.
var sequence = '';
var timeout;
var reset_s = function() {
sequence = '';
}
$(document).on("keypress", function(event) {
@A35G
A35G / convert_csv_to_vcf.php
Last active January 17, 2016 12:29
CSV to VCF - Convert file for import Contacts
<?php
$csv_file = __DIR__."/contacts.csv";
/**
* Structure of the CSV file
* "Title","First Name","Middle Name","Last Name","Suffix","Company","Department","Job Title","Business Street","Business Street 2","Business Street 3","Business City","Business State","Business Postal Code","Business Country/Region","Home Street","Home Street 2","Home Street 3","Home City","Home State","Home Postal Code","Home Country/Region","Other Street","Other Street 2","Other Street 3","Other City","Other State","Other Postal Code","Other Country/Region","Assistant's Phone","Business Fax","Business Phone","Business Phone 2","Callback","Car Phone","Company Main Phone","Home Fax","Home Phone","Home Phone 2","ISDN","Mobile Phone","Other Fax","Other Phone","Pager","Primary Phone","Radio Phone","TTY/TDD Phone","Telex","Account","Anniversary","Assistant's Name","Billing Information","Birthday","Business Address PO Box","Categories","Children","Directory Server","E-mail Address","E-mail Type","E-mail Display Name","E-mail 2 Address","
@A35G
A35G / Base_url.js
Last active September 3, 2015 11:15
Get the address of the website or web application
function getBaseURL() {
var url = location.href;
var baseURL = url.substring(0, url.indexOf('/', 14));
if (baseURL.indexOf('http://localhost') != -1) {
var url = location.href;
var pathname = location.pathname;
var index1 = url.indexOf(pathname);
@A35G
A35G / customErrorHandler.php
Created May 27, 2015 16:08
Custom Script to manage Error Log in PHP
<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno))
return true;
switch ($errno) {
case E_USER_ERROR: