Skip to main content
JR Boulay
Community Expert
Community Expert
February 19, 2019
Answered

Use previous value of "A"

  • February 19, 2019
  • 4 replies
  • 1277 views

Hi JavaScript experts.

I need to use the previous value of "A" in a function but my brain isn't enought logical to find an easy way by itself.

function myFunction(A) {

    // 1. blablabla, use A in one or several lines

    // 2. here I need to keep the value of A for the next time the function will be called

}

Thank you for your help.

This topic has been closed for replies.
Correct answer try67

Something like this?

var prevA;

function goThere(newA) {

    prevA = newA;

    this.pageNum = newA;

}

function goBack() {

    this.pageNum = prevA;

}

No. This is the first test I made.

In this case goBack will always go to the current page, since prevA is always equal to newA.

What I want to achieve:

User clic goThere(4) to go to page 4.

User clic goThere(6) to go to page 6.

User clic goThere(9) to go to page 9.

User clics goBack ==> he should go to page 6

I just need one goBack, not several.

(Don't ask me why, but I cannot use app.goBack() for this document.)


So it's not the previous value of A, it's the one before that...

You can do it like this:

var prevA = [];

function goThere(newA) {

    prevA.push(newA);

    this.pageNum = newA;

}

function goBack() {

    if (prevA.length>=2) this.pageNum = prevA[prevA.length-2];

    else console.println("can't go back.");

}

4 replies

Thom Parker
Community Expert
Community Expert
February 19, 2019

Hi JR, do you want the value of A saved permanently in the PDF, or just for the current session?

The easiest and most straight forward solution is to use a custom document property

Like this

function goThere(newA) {

    this.prevA = newA;

    this.pageNum = newA;

}

Now A is saved in a document level property.

If you want it saved permanently, then use a hidden field.

Cheers,

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
February 20, 2019

Hi JR, do you want the value of A saved permanently in the PDF, or just for the current session?

I want this value to be saved until user go to another page (when the value should be replaced by a newer).

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
February 19, 2019

Thank you both but it does not meet my needs, I will try to explain myself better.

In fact there are 2 functions :

function goThere(A) {

this.pageNum = A;

}

//

function goBack() {

this.pageNum = B;

}

Where B is the previous value of A, not the latest value. Otherwise it will goBack on the current page.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
February 19, 2019

Something like this?

var prevA;

function goThere(newA) {

    prevA = newA;

    this.pageNum = newA;

}

function goBack() {

    this.pageNum = prevA;

}

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
February 20, 2019

Something like this?

var prevA;

function goThere(newA) {

    prevA = newA;

    this.pageNum = newA;

}

function goBack() {

    this.pageNum = prevA;

}

No. This is the first test I made.

In this case goBack will always go to the current page, since prevA is always equal to newA.

What I want to achieve:

User clic goThere(4) to go to page 4.

User clic goThere(6) to go to page 6.

User clic goThere(9) to go to page 9.

User clics goBack ==> he should go to page 6

I just need one goBack, not several.

(Don't ask me why, but I cannot use app.goBack() for this document.)

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
February 19, 2019

Another approach is to use a closure, which avoids the problem of a global variable like TSN suggested from being changed by code elsewhere. This subject can be a bit complicated, but there are plenty of tutorials out there, such as: JavaScript Function Closures

Legend
February 19, 2019

You can save a value in a variable but (a) don't use "var" and (b) define it OUTSIDE your function first.

Info about variable scope in JavaScript: Demystifying JavaScript Variable Scope and Hoisting

mySavedVariable = -99999999 ;

function myStuff(A)

{

  // do things

  if ( A != mySavedVariable ) { // do things if A is different from last time

  }

  mySavedVariable = A ; // save A for next time

}