TomC Posted June 7, 2009 Share Posted June 7, 2009 Hi all - hope somebody can help with this. I'm trying to create a random string of text with determined characters. The script below works great, but uses a form to call it. I want the string to show on page load, but cant work out how to alter the JS to make that happen. I thought if I just omitted the function and form lines it should call immediately, but this doesnt seem to work. <script language="javascript" type="text/javascript">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); } document.randform.randomfield.value = randomstring;}</script>PS I have worked throught the tutorials, but still cant sort it. Thanks Link to comment Share on other sites More sharing options...
jeffman Posted June 7, 2009 Share Posted June 7, 2009 Put this at the bottom of your script, NOT inside a function.window.onload = randomString; Link to comment Share on other sites More sharing options...
TomC Posted June 9, 2009 Author Share Posted June 9, 2009 Put this at the bottom of your script, NOT inside a function.window.onload = randomString; hi Deirdre's Dad.Sorry to be a bit dense. Do you mean I should leave my script exactly as is, and add window.onload = randomString outside of the function brackets? Also I've placed all the script inside the head. Is that correct?thanks Link to comment Share on other sites More sharing options...
jeffman Posted June 9, 2009 Share Posted June 9, 2009 Yes.And golly, I hope you didn't wait for my answer before experimenting. Link to comment Share on other sites More sharing options...
TomC Posted June 9, 2009 Author Share Posted June 9, 2009 OK, I feely admit to being a dunce - but please believe I have spent hours on this, and now have quite an impressive headache!I now have:<script language="javascript" type="text/javascript">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); } document.randform.randomfield.value = randomstring;}window.onload = randomString;</script>With this in the head, I get "document.randform etc is null, or not an object"If I take that line out (as there is now no randform in the body), the script doesnt call. If I leave it in, I still cant get a result no matter where I put the line "window.onload = randomString;"I also tried using, in the body: <script type="text/javascript">document.write(randomString())</script>but this doesn't seem to work either. I'm obviously still wide of the mark, but I cant work out why. Grateful for any further help please, Link to comment Share on other sites More sharing options...
chibineku Posted June 9, 2009 Share Posted June 9, 2009 If the problem is with randform returning a null value, then why not try referencing it differently:document.getElementById("randfield").value=or document.Forms[0].randfield.value= Link to comment Share on other sites More sharing options...
justsomeguy Posted June 9, 2009 Share Posted June 9, 2009 You don't have your function returning a value. It calculates the random string, but it doesn't do anything with it. Either put an element on the page where you write the string to (apparently document.randform.randomfield doesn't exist), or return the value so that you can use your document.write statement. 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;} Link to comment Share on other sites More sharing options...
TomC Posted June 9, 2009 Author Share Posted June 9, 2009 OK justsomeguy, that does it fine! Much better than paracetamol.Big thanks for to all for your helpful advice. Still cant help wondering why I couldnt make the other solution work.... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 9, 2009 Share Posted June 9, 2009 I don't know which solution you're referring to, but if you want to write the string to an element instead of return it, you can do that too. You just need an element with a certain ID.document.getElementById("rand").innerHTML = randomstring;<div id="rand"></div> Link to comment Share on other sites More sharing options...
TomC Posted June 10, 2009 Author Share Posted June 10, 2009 Ok, got that. Actually I now have a new problem, in that I dont want to repeat any characters within any given string. I think this may prove a bit more tricky to solve - I'd be interested in your comments. Have posted fresh query,Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.