Skip to main content
Participating Frequently
April 14, 2010
Question

prevent function access between 2 scripts

  • April 14, 2010
  • 1 reply
  • 367 views

Hello,

I have a understanding problem with InDesign JavaScript Engine.

I have two simple Scripts.

Script 1:

alert(random(100));

function random (max) {
    return Math.floor (Math.random() * max);
}

Script 2:

alert(random(100));

If I execute the Script 1 it is works fine.

If I execute the Script 2, it finds the function "random" from the Script 1.

How I can prevent this ?

I have tested #targetengine, #engine and #script.

mfg

mpd323

This topic has been closed for replies.

1 reply

Marc Autret
Legend
April 14, 2010

If you execute Script1, then Script2, in the same persistent engine, the function random that you declare in Script1 is available in Script2.

What's the problem?

Do you need to use a different random function in Script2?

You may add a #targetengine 'Script1' directive in Script1, and a #targetengine 'Script2' directive in Script2, to separate each process. (In this case, random need to be defined in Script2).

If you use the same #targetengine, you can ‘overwrite’ random in Script2 by using something like:

this.random = function() {/*Script2 random stuff*/}

but that's probably not what you want...

Finally, what don't you use random1 in Script1 and random2 in Script2?

If you really need two distinct functions, create two distinct functions!

mpd323Author
Participating Frequently
April 14, 2010

Hello Marc,

this was a little sample with random(). I have a lot of Scripts. And they have functions with same Names. And some functions are little different. The JavaScript Engine use sometimes the functions from Script1 and sometimes from Script2. I have tested with #targetengine and it is the same result.