iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register

Go Back   WebForumz.com > Blogs > spinal007
Register All Albums Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Random String in Javascript

Posted April 21st, 2008 at 10:18 AM by spinal007
Code:
String((new Date()).getTime()).replace(/\D/gi,'')
That's the quick way to automatically generate a random text string in javascript - a new string every millisecond.

It's a shame that in a time of "Web 2.0" and advanced ajax-powered web interfaces we, developers, still have to deal with querky browser issues pretty much all the time - most of the in IE.

Sometimes IE caches responses in the HTTPRequest object when it shouldn't, so ajax developers frequently add random characters to the query string to get around this issue. This code helps you do just that with very little processing.

Of course, I don't want to type the same code over and over again so I took it further and turned it into a neat little function:
Code:
 function rnd(){ return String((new Date()).getTime()).replace(/\D/gi,'') }
...so all I have to do is...
Code:
 var url = '/stuff/url/?'+rnd();
// do ajax stuff...
Happy javascript'ing!

Total Comments 1

Comments

Old
Rob's Avatar
Neat!
permalink
Posted July 2nd, 2008 at 12:17 PM by Rob Rob is offline
 
Recent Blog Entries by spinal007

Search Engine Optimization by vBSEO 3.2.0 RC8