Advertisement
a1ananth

Untitled

Apr 18th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Predefined text to fill in textboxes
  2. var textboxTexts = new Array();
  3. textboxTexts.push("Hey, hey, temme yern't an a'hole yo!");
  4. textboxTexts.push("I like to spam. But hey, isn't that illegal?");
  5. textboxTexts.push("See, I don't want to hurt you mo-fo (if you know what I mean).");
  6. textboxTexts.push("Now pull down your pants and turn back. Make it quick, yo!");
  7.  
  8. var emails = new Array();
  9. emails.push("[email protected]");
  10.  
  11. var phnos = new Array();
  12. phnos.push("9876543210");
  13.  
  14. // Function to load external script, required to load jQuery
  15. function loadScript(url, callback)
  16. {
  17.    var head = document.getElementsByTagName('head')[0];
  18.    var script = document.createElement('script');
  19.    script.type = 'text/javascript';
  20.    script.src = url;
  21.  
  22.    script.onreadystatechange = callback;
  23.    script.onload = callback;
  24.  
  25.    head.appendChild(script);
  26. }
  27.  
  28. // Call above function to load jQuery
  29. loadScript(location.protocol + "//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", execMyScript);
  30.  
  31. // Callback function, executed after loading JQuery
  32. function execMyScript() {
  33.     var checkboxes = {}, radios = {}, combos = {};
  34.  
  35.     // Fill textboxes with predefined text, and collect the radios and checkboxes to fill them randomly later
  36.     $.each($('input'), function(key, value) {
  37.         var type = $(value).attr('type');
  38.         if (type == 'text') {
  39.             // Textboxes
  40.             var label = $('label[for=' + $(value).attr('id') + ']').html();
  41.            
  42.             // Check the label if the textbox is for an email or a mobile no, fill it appropriately
  43.             if (label.indexOf('Email') != -1 || label.indexOf('E-mail') != -1 || label.indexOf('e-mail') != -1 || label.indexOf('email') != -1) {
  44.                 var random = Math.floor((Math.random()* emails.length ));
  45.                 $(value).val(emails[random]);
  46.             } else if (label.indexOf('Phone') != -1 || label.indexOf('phone') != -1 || label.indexOf('mobile') != -1 || label.indexOf('Mobile') != -1 || label.indexOf('Contact') != -1) {
  47.                 var random = Math.floor((Math.random()* phnos.length ));
  48.                 $(value).val(phnos[random]);
  49.             } else {
  50.                 var random = Math.floor((Math.random()* textboxTexts.length ));
  51.                 $(value).val(textboxTexts[random]);
  52.             }
  53.         } else if (type == 'radio') {
  54.             // Radio buttons, collect them for filling later
  55.             var name = $(value).attr('name');
  56.             var id = $(value).attr('id');
  57.             if (radios[name] !== undefined) {
  58.                 radios[name].push(id);
  59.             } else {
  60.                 radios[name] = new Array(id);
  61.             }
  62.         } else if (type == 'checkbox') {
  63.             // Checkboxes, collect them for filling later
  64.             var name = $(value).attr('name');
  65.             var id = $(value).attr('id');
  66.             if (checkboxes[name] !== undefined) {
  67.                 checkboxes[name].push(id);
  68.             } else {
  69.                 checkboxes[name] = new Array(id);
  70.             }
  71.         }
  72.     });
  73.  
  74.     // Fill collected radios randomly
  75.     $.each(radios, function (key, value) {
  76.         // Generate a random number, and check a radio button
  77.         var random = Math.floor((Math.random()* value.length ));
  78.         $('#' + value[random]).attr('checked', 'checked');
  79.     });
  80.  
  81.     // Fill collected checkboxes randomly
  82.     $.each(checkboxes, function (key, value) {
  83.         // First generate a random no to decide how many checkboxes to check (for each question)
  84.         var rndcount = Math.floor((Math.random()* value.length ) + 1);
  85.        
  86.         // Generate that many random numbers and check the checkboxes
  87.         for (var i = 0; i < rndcount; i++) {
  88.             var random = Math.floor((Math.random()* value.length ));
  89.             $('#' + value[random]).attr('checked', 'checked');
  90.         }
  91.     });
  92.  
  93.     // Fill textareas with textbox text
  94.     $.each($('textarea'), function(key, value) {
  95.         var random = Math.floor((Math.random()* textboxTexts.length ));
  96.         $(value).val(textboxTexts[random]);
  97.     });
  98.  
  99.     console.log("Filled all I could fill!");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement