Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
I developed a simple app that would choose a random quote and display it in a div. Using arrays and these bits of code:
function WriteText(arraynum)
{
switch(arraynum)
{
case 1:
var ArrLen=SprQuotes.length;
  var ArrNum=(Math.floor(Math.random()*ArrLen));
  var txt=SprQuotes[ArrNum];
  break;
	
case 2:
var ArrLen=HisQuotes.length;
  var ArrNum=(Math.floor(Math.random()*ArrLen));
  var txt=HisQuotes[ArrNum];
  break;
	
case 3:
var ArrLen=LitQuotes.length;
  var ArrNum=(Math.floor(Math.random()*ArrLen));
  var txt=LitQuotes[ArrNum];
  break;
  
case 4:
var ArrLen=RelQuotes.length;
var ArrNum=(Math.floor(Math.random()*ArrLen));
var txt=RelQuotes[ArrNum];
break;

case 5:
var ArrLen=PresQuotes.length;
var ArrNum=(Math.floor(Math.random()*ArrLen));
var txt=PresQuotes[ArrNum];
break;

case 6:
var ArrLen=MiscQuotes.length;
var ArrNum=(Math.floor(Math.random()*ArrLen));
var txt=MiscQuotes[ArrNum];
break;

default:
  var txt="";
  var ArrLen=0;
}

document.getElementById('text1').innerHTML=txt;

}

And in the body
<img src="images/book.jpg" width="276" height="300" usemap="#bookmap" class="usi"/></center>
<map name="bookmap">
		 <area shape="rect" coords="0,0,276,50"  onclick="WriteText(5)"/>
		 <area shape="rect" coords="0,50,276,100"  onclick="WriteText(2)"/>
		 <area shape="rect" coords="0,100,276,150"  onclick="WriteText(3)"/>
		 <area shape="rect" coords="0,150,276,200"  onclick="WriteText(4)"/>
		 <area shape="rect" coords="0,200,276,250"  onclick="WriteText(1)"/>
		 <area shape="rect" coords="0,250,276,300"  onclick="WriteText(6)"/>
</map>
<p>
<div style="width:300px;height:600px;top:360px;margin:auto;" id="text1"></div>
<p>



I was able to get the strings saved in the arrays to display when the areas of the image were clicked. However, after filling in thousands of quotes into the arrays, this no longer works. No text is displayed in the div. Can anyone see why this would stop working? I didn't modify anything in the WriteText function after it was working, and the imagemap is recognized when I test it on my phone. I kept the syntax of the arrays the exact same when switching from dummy text to quote text.
Posted
Updated 20-Dec-11 9:23am
v4

You haven't shown all the Javascript on the page, so it's hard to be sure. In general, when something that was working stops working when you haven't changed it then the problem is somewhere else.

My guess is that somewhere in your arrays of quotes you have something that is causing a syntax error - maybe an unescaped single or double quote (depending on how you are quoting your strings) or a missing comma.
 
Share this answer
 
Thanks for the reply. Here is an example of a quote:

PresQuotes[1524]="A good compromise, a good piece of legislation, is like a good sentence; or a good piece of music. Everybody can recognize it. They say, 'Huh. It works. It makes sense.' -Barack Obama";

Does the semi-colon inside the quote affect the coding? How about the single quotes inside the string? Could that be causing the issue? If so, how would I go about marking a quotation inside a string?
 
Share this answer
 
Comments
Graham Breach 21-Dec-11 6:17am    
Nothing inside that string should cause trouble. Quoting problems happen when you have the same quote marks inside the string as you use to delimit the string.

Example: "They say, "Huh. It Works. It makes sense."" - the inner quotes have to be escaped with a backslash: "They say, \"Huh. It Works. It makes sense.\""
Member 8504796 21-Dec-11 9:32am    
After disassembling the code, the problem lies either in a syntax error within one of the quotes, or the large number of quotes is confusing the length and random portions of the function. Thanks for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900