1// Random number insertion library 2// requires jQuery 3// 4// put this in design xml file 5// <JSInclude library="random.js" /> 6// 7// you call it as: 8// Random.image(6, '<img id="photos" src="{$imagePath}/photos1.jpg" alt="photos of Otsego, MN" />'); 9// or 10// Random.className(6, 'myBox'); 11// 12// in the className method, default class on the box should always be "random1" to 13// provide a default and prevent a FOUC 14 15var Random = new (function Random() { 16 17 // PUBLIC METHODS 18 19 // * use "cram" to force a number, rather than use a random one 20 // * each returns the number it selected 21 22 // takes a number and a string containing a valid, default image 23 // tag, randomizes the numeral "1" in the last pathname piece and 24 // spits out the finished tag 25 this.image = function randomImage(max, imageTag, cram) { 26 var $img = $(imageTag); 27 var n = (cram != null ? setSrc(cram, $img) : randomSrc(max, $img)); 28 // put it in a dummy shell so we can get its contents as an 29 // HTML string 30 document.write(($("<div />").append($img)).html()); 31 document.close(); 32 33 return n; 34 }; 35 36 // adds a class to the given element called "randomX", where X is the random integer 37 this.className = function randomClassName(max, id, cram) { 38 var $e = $("#" + id); 39 var n = (cram != null ? cram : randomNumber(max)); 40 if ($e.hasClass("random1")) { 41 $e.removeClass("random1"); 42 $e.addClass("random" + n); 43 } 44 45 return n; 46 }; 47 48 // replaces "random1" in the style att. on all elements matching the selector 49 this.style = function randomStyle(max, selector, cram) { 50 var $e = $(selector); 51 var n = (cram != null ? cram : randomNumber(max)); 52 $e.each(function() { 53 $(this).attr("style", $(this).attr("style").replace("random1", "random" + n)); 54 }); 55 56 return n; 57 }; 58 59 60 61 // INTERNAL FUNCTIONS 62 63 // replaces the numeral "1" with the requested random number in an 64 // image's src attribute 65 function randomSrc(max, element) { 66 var n = randomNumber(max); 67 setSrc(n, element); 68 return n; 69 } 70 71 function setSrc(n, element) { 72 var $img = (typeof element == "string" ? $("#" + id) : $(element)); 73 var newSrc = $img.attr("data-src") ? $img.attr("data-src") : $img.attr("src"); 74 var lastslash = newSrc.lastIndexOf('/'); 75 var part1 = newSrc.substr(0, lastslash); 76 var part2 = newSrc.substr(lastslash); 77 78 part2 = part2.replace('1', n); 79 $img.attr('src', (part1 + part2)); 80 81 return n; 82 } 83 84 // returns a random number between 1 and max 85 function randomNumber(max) { 86 return Math.ceil(Math.random() * max); 87 } 88 89 // test harness for randomness 90 function random_testDistribution() { 91 var arr = [0,0,0,0,0]; 92 for(i = 1; i < 10; i++) { 93 var rand = randomNumber(5); 94 arr[rand - 1]++; 95 } 96 console.log(arr); 97 } 98})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.