FlyingIsFun1217 Posted October 12, 2009 Share Posted October 12, 2009 Hey, here's what I've got. Seems to work in IE, but not Firefox. Any clue? <script language="javascript"> function random_passwd() { chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*"; password = ""; for(x=0;x<9;x++) { i = Math.floor(Math.random() * 79); password += chars.charAt(i); } return password; } function formSubmit() { generatorForm.passwdBox.value = random_passwd(); return false; } </script> <form name="generatorForm"> <input name="passwdBox" type="text" /> <input type="button" value="Generate New Password" onClick="javascript:formSubmit()" /> </form> Seems that it works in Chrome, IE, but not Firefox. Is there some little Javascript snippet that Firefox does not support currently? Thanks! FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
xenophobia Posted October 13, 2009 Share Posted October 13, 2009 Try: <input type="button" value="Generate New Password" onClick="formSubmit()" /> without the javascript infront of it. If it still doesn't work, try use alert statement to debug which line the statement is not working, eg: function formSubmit() { alert("This function is called."); generatorForm.passwdBox.value = random_passwd(); return false; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2009 Share Posted October 13, 2009 It works for me in FF, but there are some problems in the code. You only have a string of 70 characters, yet you are generating random numbers from 0 to 78 (i.e. 79 characters). The function should be dynamic based upon the number of characters in the string. I would also make the length of the password dynamic with a min and max length, say from 9 to 15 characters. Also, you don't need to return false when using a non-submit button. <script language="javascript"> function random_passwd(min, max) { var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*'; var length = min + Math.floor(Math.random() * (max-min)) var password = ''; for(var i=0; i<length; i++) { password += chars.charAt(Math.floor(Math.random() * chars.length)); } return password; } function formSubmit() { generatorForm.passwdBox.value = random_passwd(9, 15); return false; } </script> <form name="generatorForm"> <input name="passwdBox" type="text" /> <input type="button" value="Generate New Password" onClick="formSubmit()" /> </form> Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted October 13, 2009 Author Share Posted October 13, 2009 I've uploaded my example to http://www.ploronex.com/it109/password.html. I still can't see what's causing a problem at all, and it seems when I change 79 to a lower value, it doesn't work in other browsers. Man, this is starting to confuse me. Guess that's what I get when I don't know javascript well. Thanks for any further guidance, I appreciate it! FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2009 Share Posted October 13, 2009 OK, the problem is how you are referencing your input object. You are not stating it in the appropriate context. It doesn't know where to find the "generatorForm" object. You should use: document.generatorForm.passwdBox.value = random_passwd(); I see no reason why that code would not work with numbers lower than 79. Besides, as I stated you don't need to hard code that value, just use the length of the variable with the characters. Full code that should work: <h3>Secure Password Generator</h3> <script language="javascript"> function random_passwd(min, max) { var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*'; var length = min + Math.floor(Math.random() * (max-min)) var password = ''; for(var i=0; i<length; i++) { password += chars.charAt(Math.floor(Math.random() * chars.length)); } return password; } </script> <form name="generatorForm"> <input name="passwdBox" type="text" value=""/> <input type="button" value="Generate New Password" onClick="this.form.passwdBox.value = random_passwd(9, 13);" /> </form> Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted October 13, 2009 Author Share Posted October 13, 2009 Ok, I see what's going on now. Any reason Firefox is the only browser that can't fix my mistake? Seems odd that it wouldn't be able to make up for it like all of the other browsers. Thanks! FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 13, 2009 Share Posted October 13, 2009 Not to be rude, but why should it fix your mistakes? Just because IE makes an assumption of what it thinks you want and it works as you would like doesn't make it valid code. Because it is invalid code there is no standard on how it should be interpreted. The browsers that "work" with your code may have different logic for "fixing" these types of errors and it just happened, in this instance, that they got it right - this time. It could just as well of been that each could have interpreted the invalid code differently resulting in wildly different results. Think about all the time you have wasted on this because you developed in one application that interprets something differently than another application. If they all follow the same standard you don't need to do as much testing between browsers. I for one would rather have an application not work with invalid code - making me fix it - than having it work with my code but not working in the other applications. Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted October 15, 2009 Author Share Posted October 15, 2009 Not to be rude, but why should it fix your mistakes? Just because IE makes an assumption of what it thinks you want and it works as you would like doesn't make it valid code. I'm not saying it should, I actually think the other browsers are "in the wrong", so to speak. My only interest is that every other browser seems to fix it. Makes it seem like Firefox is not doing things right when it works with everything else. FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
haku Posted October 15, 2009 Share Posted October 15, 2009 There is no doing it right or wrong as far as javascript is concerned unfortunately, as there is no properly defined standard. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.