<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h1>Exemple de sauvegarde/chargement de hi-scores dans LocalStorage</h1
<p>Tester cet exemple en stand alone (flèche noire en haut à droite dans l'onglet output),
puis ouvrir les devtools, onglet Application, puis cliquer sur LocalStorage
dans la liste, pour voir les scores...</p>
<button onclick="genereHiScore()">Cliquer pour générer un Hi-Score</button>
<br>
<button onclick="sauveHiScores()">Sauvegarder Hi Scores</button>
<br>
<button onclick="chargeHiScores()">Charger les Hi Scores sauvegardés</button>
<br>
<button onclick="resetHiScores()">Remettre à zéro les Hi Scores</button>
<h2>Hi scores</h2>
<table id="tableHiScores">
<tr>
<th>Nom</th>
<th>Score</th>
<th>Level</th>
</tr>
<tbody id="tableHiScoresBody">
<tr>
<td>Michel Buffa</td>
<td>12000</td>
<td>5</td>
</tr>
</tbody>
</table>
</body>
</html>
table {
width:100%;
}
table, th, tr, td {
border:1px solid black;
padding:5px;
border-collapse:collapse;
}
td {
text-align:center;
}
let hiScores = [];
let tableHTMLHiScores;
var tableBodyHTMLHiScores;
window.onload = init;
function init() {
// appelée quand la page est chargée
tableHTMLHiScores=document.querySelector("#tableHiScores");
tableBodyHTMLHiScores=document.querySelector("#tableHiScoresBody");
}
function genereHiScore() {
// 1 - On génère un score
const tabJoueurs = ["Michel", "Emmanuelle", "Fabrice"];
let score = {
nom:tabJoueurs[Math.floor(Math.random()*3)],
score: Math.floor(100 + Math.random()*20000),
level: Math.floor(Math.random()*8)
}
// On ajoute le nouveau score au tableau JavaScript des hi scores
hiScores.push(score);
// On affiche une ligne supplémentaire dans le tableau
ajouteScoreDansTable(score)
}
function ajouteScoreDansTable(score) {
// 2 - On l'insère comme une ligne du tableau,
// attention on insère dans le <tbody> !
let row = tableBodyHTMLHiScores.insertRow();
// Une manière de faire est d'utiliser un template JS, pratique, on
// peut l'écrire sur plusieurs lignes. On aurait pu utiliser l'API
// des tables HTML5 avec row.insertCell() etc. Ou tout faire avec
// innerHTML sans utiliser du tout les inserRow/Cell...
row.innerHTML=`
<td>${score.nom}</td>
<td>${score.score}</td>
<td>${score.level}</td>
`;
}
function afficheHiScoresDansTable() {
// On remet à zéro la table HTML
tableBodyHTMLHiScores.innerHTML = "";
hiScores.forEach(s => {
ajouteScoreDansTable(s);
})
}
function sauveHiScores() {
localStorage.hiScores = JSON.stringify(hiScores);
}
function chargeHiScores() {
if(localStorage.hiScores) {
hiScores = JSON.parse(localStorage.hiScores);
afficheHiScoresDansTable();
}
}
function resetHiScores() {
hiScores = [];
// efface toutes les données dans localStorage
localStorage.clear();
// Si on voulait juste effacer les hi scores
//localStorage.hiScores = [];
// Pour effacer le tableau affiché
// On remet à zéro la table HTML
tableBodyHTMLHiScores.innerHTML = "";
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |