2016-09-09 2 views
0

Ich habe einen Passwort-Generator, der gut funktioniert. Aber brauche ein wenig Abwechslung. Das folgende Bild zeigt enter image description hereAdd Array-Werte ohne vorherige Wert mit JavaScript löschen

Sobald ich auf die Schaltfläche "Passwort generieren" klicke, erzeugt es ein Passwort. Erforderlich: Wenn ich erneut auf die Schaltfläche klicke, muss unten ein anderes Passwort generiert werden, ohne das vorherige zu löschen. Ich habe ein paar Variationen in der Schleife versucht, aber nicht funktioniert.

**passGen.js** 

function passGen() { 
var Generator = {}; 
Generator.generateMnemonic = function(length, num_length, mixcase) { 
var ret = ''; 
var vowels = 'aeioe'; 
var consonants = 'bcdfghklmnpqrstvwxzy'; 
if (mixcase) { 
    vowels += vowels.toUpperCase(); 
    consonants += consonants.toUpperCase(); 
} 
vowels = vowels.split(''); 
consonants = consonants.split(''); 

for(var i = 0; i < length/2; i++) { 
    ret += vowels.getRandom(); 
    ret += consonants.getRandom(); 
} 

if (!num_length) return ret; 

var pos = $random(2, length - 2 - num_length); 
return ret.substr(0, pos) + $random(Math.pow(10, num_length - 1), Math.pow(10, num_length) - 1) + ret.substr(pos + num_length); 
    }; 
    var observe = new Observer('#generator-length, #generator-num_length, #generator-mixcase, #generator-amount', function(values) { 
    var length = values[0].toInt(); 
    var num_length = values[1].toInt(); 
    var mixcase = values[2].toInt(); 
    var amount = values[3].toInt(); 

    // Fill passwords in a loop 
    var words = []; 
    for (var i = 0; i < amount; i++) { 
     words.push(Generator.generateMnemonic(length, num_length, mixcase) ); 

    } 

    // Get the output area 
    var output = $('generator-output'); 

    // Output it and highlight it so users will notice the update 
    output.value = words.join("\n"); 
    output.getParent().highlight('#ff8', '#fff'); 


    }, { 
    // periodical: 1000 // interval in ms 
    }); 

    // To fill in the first values 
    observe.fire(); 
    } 
    **Part of Hmtl** 
<script type="text/javascript" src="passGen.js"></script> 
<span>How many passwords:</span> 
    <br> 
    <select name="amount" id="generator-amount"> 
     <option value="1" selected>1</option> 
     <option value="5">5</option> 
     <option value="10">10</option> 
     <option value="50">50</option> 
     <option value="100">100</option> 
    </select> 
</label> 
<input type="button" name="button" value="Generate Password" onclick="passGen();"> 
<label> 
<br> 
    <span>Your passwords:</span> 


+0

Könnten Sie bitte ein anschauliches Beispiel posten? – Li357

+0

Können Sie eine jfiddle für das gleiche erstellen? – xCodeZone

Antwort

0

etwas in dieser Richtung tun: mit einer statischen Variablen (kleines Beispiel das Gefühl zu geben).

function passGen() { 
 
if (typeof passGen.words == 'undefined') { /* It has not been called do initialization*/ 
 
passGen.words = [];}//else previous passwords persist, and you push, onto them. 
 
passGen.words.push("Hello"); 
 
alert(passGen.words); 
 
} 
 
passGen(); 
 
passGen();

In Ihrem Fall meine erste halten, wenn, entfernen Sie Ihre Linie

var words = []; 

und prepend PassGen. auf Ihre words.push und words.join

von Static variables in JavaScript angepasst

Verwandte Themen