i'm using switch statement load different images , text on 404 page , working wondering if there better way write this? shorthand it?
<script> var funfacts = math.floor(math.random() * 7) + 1; switch (funfacts) { case 1: document.getelementbyid("funfacttext").innerhtml = "in florida, against law put livestock in school bus."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff1.jpg")"; break; case 2: document.getelementbyid("funfacttext").innerhtml = "in texas, it's against law have pair of pliers in or possession."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff2.jpg")"; break; case 3: document.getelementbyid("funfacttext").innerhtml = "in alaska, illegal @ moose window of aircraft or flying vehicle. illegal push live moose out of moving aircraft."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff3.jpg")"; break; case 4: document.getelementbyid("funfacttext").innerhtml = "in ohio, women prohibited wearing patent leather shoes in public."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff4.jpg")"; break; case 5: document.getelementbyid("funfacttext").innerhtml = "by law, in vermont must take @ least 1 bath week."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff5.jpg")"; break; case 6: document.getelementbyid("funfacttext").innerhtml = "in illinois, law car must driven steering wheel."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff6.jpg")"; break; case 7: document.getelementbyid("funfacttext").innerhtml = "california law prohibits woman driving car while dressed in housecoat."; document.getelementbyid("funfactimg").src = "@url.content("~/themes/pg/content/images/ff7.jpg")"; break; } </script>
yeah, use array facts, , simple string concatenation:
var funfacts = [ "in florida, against law put livestock in school bus.", "in texas, it's against law have pair of pliers in or possession.", "in alaska, illegal @ moose window of aircraft or flying vehicle. illegal push live moose out of moving aircraft.", "in ohio, women prohibited wearing patent leather shoes in public.", "by law, in vermont must take @ least 1 bath week.", "in illinois, law car must driven steering wheel.", "california law prohibits woman driving car while dressed in housecoat." ]; var funfact = math.floor(math.random() * 7); document.getelementbyid("funfacttext").innerhtml = funfacts[funfact]; var basesrc = "@url.content("~/themes/pg/content/images/ff")"; document.getelementbyid("funfactimg").src = basesrc + (funfact + 1) + '.jpg';
Comments
Post a Comment