0

I want to show a dialog box containing a random code I make. This is my function:

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    alert(randomstring);
    window.location = "Insert-Transac.jsp";
}

I want to show the variable randomstring for a time and hide it. Can any one help me?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    Hint: You won't be able to use `window.alert` for this. Check out various jQuery popup libraries, such as [Apprise](https://github.com/ThrivingKings/Apprise). – Mike Christensen Apr 26 '13 at 21:17
  • 3
    Start with making the function return `randomstring` and not alerting nor redirecting. Then inject the string into some HTML element, and hide it with a timer. – bfavaretto Apr 26 '13 at 21:17

1 Answers1

0

Would this work?

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}
document.getElementById("randstring").innerHTML = randomString();
window.setTimeout(function(){document.getElementById("randstring").innerHTML = ""}, 2000);

This assumes you have an element with the ID of randstring.

y--
  • 588
  • 2
  • 10
  • 27
  • thanks alot gordan but i want to ask you a question id dint want to redirect to another page a want to show this randon string in a dialog then close this dialog after some seconds can you help me – user2325383 Apr 26 '13 at 21:42
  • Hmmm, let me think about this. – y-- Apr 26 '13 at 21:51