Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to cut short an alert that is way too long

Status
Not open for further replies.

ttomasko

Programmer
Aug 6, 2008
28
Hello,

I often have to display the elements of an array in an alert box (probably not a good thing on the web but this is for the InDesign program).

When there are many elements this creates an alert box that is so long that one cannot find the bottom in order to dismiss the alert.

Below is a script that allows only for displaying the first 10 elements. I am just wondering if there is a shorter or better way to write this.

Thanks,
Tom

Code:
//var aa = [1,2,3,4,5,6];
var aa = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
var alertText = "";
var cutOffAlert = "";
if(aa.length <= 9){
		lessThan9();
		}//end if
if(aa.length > 9){
		moreThan9();
		}//end if
alert(alertText);

//****functions*******
function lessThan9(){
	for(var i = 0; aa.length > i; i++){
	alertText += "Page "+aa[i]+"\r\r\r";
		}//end for i
	return alertText;
	}//end function lessThan9
function moreThan9(){
	for(var j = 0; aa.length > j; j++){
		cutOffAlert += "Page "+aa[j]+"\r\r\r";
		if(j > 8){
			break;
			}//end if
	}//end for
	alertText = "Help! This is too long to see the OK button on the bottom!\r\r"+cutOffAlert+"There are "+(aa.length-10)+" more elements. Click Yes (if this were a confirm) to get a .txt document of all of them."
	return alertText;
	}//end function moreThan9
 
Do you want to break it down to 10 alert? Each alert contains only 1 page. For ex, you can place the alert inside the for loop.
 
Kendel,

No, making more than one alert would be unmanageable.

This script is simplified and non-InDesign Object Model. The more complex version will look for certain types of errors in, say, a 100-page document. Sometimes there are just a few errors and it is no biggie to just have an alert that lists the errors and the page they are on.

But sometimes there are scores of them. Then the alert is too long. In that case I only want to display the first 10 or so to give the user an idea of what they are facing. The alert would actually be a confirm so that by clicking Yes, a .txt document would be created that lists all of the errors.

So only one alert.

I just want to know if there are other ways of writing this.

Tom
 
Hi

The same in my approach would look like this :
Code:
[b]if[/b] (aa.length<=9) {
  alert([i]'Page '[/i]+aa.join([i]'\n\n\nPage '[/i]))
} [b]else[/b] {
  [b]if[/b] (confirm([i]'Help! This is too long to see the OK button on the bottom!\n\n\nPage '[/i]+aa.[url=http://devguru.com/technologies/javascript/10557.asp]slice[/url](0,9).join([i]'\n\n\nPage '[/i])+[i]'\n\n\nThere are '[/i]+(aa.length-10)+[i]' more elements. Click Yes (if this were a confirm) to get a .txt document of all of them.'[/i])) {
    [gray]// do the .txt here...[/gray]
  }
}
By the way, in your moreThan9() function the [tt]if[/tt] and [tt]break[/tt] is pointless while you only want to loop over the first 9 elements and you are sure there are more than 9 :
Code:
for (var j = 0; [red]9[/red] > j; j++){
   cutOffAlert += "Page "+aa[j]+"\r\r\r";
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top