These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How can AcroJS only affect current page

WaveF
Registered: May 4 2008
Posts: 3
Answered

Please forgive my pool english first. I'm working on some eBooks these days, such as use a button to ctrol some answer words show and hide. The answers were printed on the page, so I put a white button on the answer(to made the answer can be seen), and ctrol this white button show or hide.

But there was the problem: when I put these buttons to different page, the button on the first page also affect other page's.

I use several buttons (btn1, btn2, btn3...) to ctrol the answer(ans1, ans2, ans3...);
the "Show / Hide all answers" button's name is "reverseBtn".

here's the code:
############################################

-------------------Document Javascripts---------------

var totalAns = 100;
var revBtn = this.getField("reverseBtn");
var showBtn = this.getField("showBtn");
var hideBtn = this.getField("hideBtn");
toShow();
 
///////////////////////////////////////////////////////////
 
function toShow()
{
    marker = true;
    revBtn.buttonSetIcon(showBtn.buttonGetIcon());
    for (var i = 1; i < totalAns; i++){
        var single = this.getField("ans" + i);
        var button = this.getField("btn" + i);
        button.userName = "Show answer";
        single.display = display.visible;
        revBtn.userName = "Show all answers";
    }
}
 
function toHide()
{
    marker = false;
    revBtn.buttonSetIcon(hideBtn.buttonGetIcon());
    for (var i = 1; i < totalAns; i++){
        var single = this.getField("ans" + i);
        var button = this.getField("btn" + i);
        button.userName = "Hide answer";
        single.display = display.hidden;
        revBtn.userName = "Hide all answers";
    }
}
 
function toShowHide(){
    if (marker == false)
        toShow();
    else
        toHide();
}
 
function reverseAns(){
    if (ans.display == display.hidden){
        btn.userName = "Show answer";
    } else {
        btn.userName = "Hide answer";
    }
    ans.display = !ans.display;
}
 
/////////////////////////////////////////
 
function checkAllAns(){
	var allShows = true;
	var allHides = true;
 
	for (var i=1; i<totalAns; i++) {
                            var checker = this.getField("ans" + i);
		if (checker) {
			if (checker.display == display.visible) {
				allHides = false;
			} else {
				allShows = false;
			}
		}
		if (allHides == false & allShows == false) {
			break;
		}
	}
	if (allShows) {
		toShow();
	}
	if (allHides) {
		toHide();
	}
}

------------the button ctrol singel answer---------------

var Num = 3;
 
var btn = getField("btn" + Num);
var ans = getField("ans" + Num);
 
reverseAns();
checkAllAns();

------------the button ctrol all answers---------------

toShowHide();
############################################

PS. every page, button's name should start from "btn1", if not so, it will be too many buttons and answers object I have to name.

in the end, I try to move 'Document Javascripts' to 'Page Actions', I thought 'Page Actions' maybe only affect current page, but I'm wrong. It seems to be no way to getField from only current page. I never write code before( I'm a designer ), but this time I have to make this eBook works, can anybody give some advice?

【There is a sample file - main code still in Document Javascripts】
http://minicg.com/PDF/page_sample.pdf

My Product Information:
Acrobat Pro 8.1.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
First, you can not call the function "toShow()" before the function is established. The call should be placed after the code for the function.

You can pass parameters when calling a function, so you modify the "toShowHide()", "toShow()", and "toHide()" functions to accept the page number a button is located on.

function toShowHide(fPageNum)
{
console.show(); console.clear();
console.println("From Page: " + fPageNum + 1);
...
}

and the call in the button would be:

toShowHide(this.pageNum);

You may also need to name your fields in such a manner as to include the page number within the field name.

George Kaiser

WaveF
Registered: May 4 2008
Posts: 3
Thanks very much, and that means I have a lot of works to do -_-b

In fact, each page have about 20 answer text, and there're about 200 pages waiting foe me, then name my fields include the page number would be... that's a time suck. I'm wondering if there's better way to deal with these 'show and hide' buttons without giving every field a name include page number? Of course, if no other way, I will give them that name.