eval

The eval function allows you to enter a string, expression or variable name that will return the result of the code, in other words it executes the code if it is valid. In the example below, imagine you have created two columns of buttons on a page: the buttons in one column set the value of the variable objName to a different object name on the page (e.g. IntroImage, SecondImage, ThirdImage etc.); the second column sets the value of the variable NextTask to an OpusScript function (e.g. Show(), Hide(), Move() etc.).

The first two lines of the code below show just two possible values for the variables objName and NextTask. Next imagine you click a different button on the page that sets the value of a variable named perform – the third line shows the value set in the perform function (i.e. the value will be the following string "IntroImage.Show()"). Finally the last line uses the eval function to execute the string as if it was a line of code – in other words, it will show the object named IntroImage on the page.

var ObjName = "IntroImage"
var NextTask = "Show()"
var perform = ObjName + "." + NextTask
eval(perform)

This example shows how you can use the eval function to execute a line of code that has been built up using strings. It is not possible "execute" a string or variable on its own because it is simply a string of text – this is only possible by using eval.

Close