🔧 Morceaux de programmes
☰
Cette page contient des morceaux de programmes qui me sont utiles.
javascript
time
var tim=new Date();
tim=tim.getTime();
console.log(Performance.now())
get select value
var domSelect=document.getElementById('mySelectId');
var value=domSelect.options[domSelect.selectedIndex].value;
create node
var parent=document.getElementById('div_parent');
var child=document.createElement('p');
child.setAttribute('data-type1','theType');
parent.appendChild(child);
child.addEventListener('click' , myFunct, false );
debounce
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
// Usage
var myEfficientFn = debounce(function() {
// All the taxing stuff you do
}, 250);
window.addEventListener('resize', myEfficientFn);
getAbsoluteUrl
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();
// Usage
getAbsoluteUrl('/something'); // ;-) https://davidwalsh.name/something
ajax call
//===============================================================================================================================
function myAjaxCall(){
var r = new XMLHttpRequest();
r.open("POST",'za_ajax.php?phpfileWithoutExtension',true);
r.timeout=6000;
r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
try{
var jsonRet=JSON.parse(r.responseText);
if(jsonRet.status=='OK'){
// do good stuff
return;
}else{
display_ajax_error_in_cons(jsonRet);
console.log(r);
alert('BAD job !');
return;
}
}catch(e){
console.error(e,r);
return;
}
};
r.onerror=function(e){console.error('e=',e); /* whatever(); */ return;}
r.ontimeout=function(e){console.error('e=',e); /* whatever(); */ return;}
var data={
funct : 'phpfileWithoutExtension',
subFunct : 'functionNameInPhpFile',
somedata1 : 'something1',
somedata2 : 'something2',
}
r.send('data='+encodeURIComponent(JSON.stringify(data)));
}
//=====================================================================================================================
function display_ajax_error_in_cons(jsonRet) {
var txt = '';
if(jsonRet.hasOwnProperty('status')){txt+='status:'+jsonRet.status+' ';}
if(jsonRet.hasOwnProperty('message')){
if(Array.isArray(jsonRet.message)){
for(var elem in jsonRet.message){
txt+=''+jsonRet.message[elem]+'\n';
}
}else{
txt+=''+jsonRet.message+'\n';
}
}
console.log(txt);
console.log('jsonRet=', jsonRet)
}
trim, htmlEntities
if(!String.prototype.trim){
String.prototype.trim=function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
if(!String.prototype.htmlEntities){
String.prototype.htmlEntities=function(){
return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
};
}
if(!String.prototype.htmlEntitiesBack){
String.prototype.htmlEntitiesBack=function(){
return this.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&amp;/g,'&');
};
}
php and js
php json to function
<?php
$param=array(
'param1' => 1 ,
'param2' => 'hello, world \' " + \\ {}\r\n=>new € line'
);
$paramUrl=json_encode($param);
$paramUrl=str_replace('\\','\\\\',$paramUrl);
$paramUrl=str_replace('\'','\\\'',$paramUrl);
$paramUrl=str_replace('"','\\"',$paramUrl);
$paramUrl=rawurlencode($paramUrl);
$o1='<a href="javascript:action(' . var_export($paramUrl,true) . ')">action';
echo $o1;
?>
<script>
function action(par){
console.log(par);
par=JSON.parse(par);
console.log(par);
par.param2=par.param2.replace(/\\n/g,'\n');
par.param2=par.param2.replace(/\\r/g,'\r');
alert(par.param2);
}
</script>
HTML
rel="noopener"
Le bon moyen d'écrire un lien avec target="_blank" est d'ajouter un rel="noopener". allez voir cet article : dareboost.comtarget="_blank" rel="noopener"
html minimal
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf8" />
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- link rel="stylesheet" href="" / -->
<style type="text/css">
html{-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;}
*, *:before, *:after {-webkit-box-sizing: inherit;-moz-box-sizing: inherit;box-sizing: inherit;margin:0;padding:0;border:0;}
body{
font-family:verdana, arial, sans-serif;
font-size:16px;
border:0px red solid;
}
*{scrollbar-color: #34d3f7 #bfe8ff;}
*::-webkit-scrollbar {width: 1.2em;background:#bfe8ff;}
*::-webkit-scrollbar-thumb {background-color: #34d3f7;}
*::-webkit-scrollbar-corner{background-color: #34d3f7;}
*::-webkit-resizer{background-color: #34d3f7;}
</style>
</head>
<body>
<img id="img1" src="https://picsum.photos/200" style="width:200px;" />
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
console.log('DOMContentLoaded first');
document.body.style.backgroundColor='#eee'; // exécuté avant le chargement complet des images
document.getElementById('img1').addEventListener('load',function(e){
console.log('image loaded comes before all loaded');
});
});
window.addEventListener('load', function () {
console.log("finally all loaded!")
})
</script>
</body>
</html>
PHP
curl get
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"); //Make this valid if possible
if(strpos($url,'https')!==false){
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($ch);
$curlinfo1=curl_getinfo($ch);
error_log( __FILE__ . ' ' . __LINE__ . ' $curlinfo1=' . var_export( $curlinfo1 , true ) );
$errors=curl_error($ch);
error_log( __FILE__ . ' ' . __LINE__ . ' $errors=' . var_export( $errors , true ) );
curl_close($ch);
Divers
Liens
- Github hugues-koolsol
- un test de popup modale
- un test de boite centrée
- un bug de textarea sur chrome ( et pas sur firefox )
- un test de rotation de carte en 3D
📝 Les Articles.
Le symbole "🔧" (clé plate) indique les articles techniques.
- ♥♠♣♦ Jeu de solitaire
- 🤔 Jeu de syllabes
- ▦ Jeu de sudoku
- 💣 Jeu du démineur
- 🧠 Jeu de mémoire
- 🟥 Jeu samegame
- ✅ Application todo
- 😎 Nicolas Boileau
- 👋 Intelligence artificielle
- 😉 Liens sympas
- 😀 Avantages des pwa
- 🔧 Classement lighthouse 💪
- 🔧 Liens techniques
- 🔧 Icones et utf-8 🤡
- 🔧 La guerre internet
- 🔧 Mises à jour des pwa
- 🔧 Morceaux de programmes
- 🔧 Formulaires de login
- 🔧 Internationalisation 😎
- 😀 À propos de ce blog
- 🎮 Les liens vers les jeux et applications