Posts

Showing posts from January, 2012

Mocking Ajax with the JQuery Mockjax Library

Unit testing your JavaScript code to ensure everything works according to plan is a very important and tricky business. Properly testing code bits can save you a lot of time and money. This article focuses not on the general topic of unit testing JavaScript, but more specifically unit testing JQuery Ajax functions. HTML File for the Test. <! DOCTYPE HTML > <html>     <head>         <link rel = "stylesheet" href = "lib/qunit.css" type = "text/css" media = "screen" />         <script src = "lib/qunit.js" ></script>         <script src = "lib/jquery-1.7.js" ></script>         <script src = "lib/json2.js" ></script>         <script src = "lib/jquery.mockjax.js" ></script>         <script src = "mockjax.js" ></script>         <title> MockJax Tests - </title>       </he

JavaScript Pseudo-Random ID Generator

Simple Random Id Generator for JavaScript Sometimes you might need a Random ID generator. The following generator takes an integer from which it will generate a string of that length. Longer string means more permutations and increased uniqueness. The following function was tested with input 15. A sample space of 5,000,000 IDs was generated and all were tested unique. function generateRandomId ( length ) {     "use strict" ;     var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" , returnValue = "" , x , i ;     for ( x = 0 ; x < length ; x += 1 ) {         i = Math . floor ( Math . random () * 62 );         returnValue += chars . charAt ( i );     }     return "ID_" + returnValue ; } NB: Increase length for increased uniqueness GenerateRandomId Java Permutation Tester (Simple test for the function above written in java) import java . ut