# What is the best way to insert document-level JavaScript in LaTeX documents

I have been toying with the idea of converting some forms at work to interactive forms. I have tried some of the routines that come with insdljs, but they seem a bit dated. The idea is very attractive. I am looking for more recent information, links and pointers. Does Beamer use JavaScript?

A similar question was posted in the German newsgroup de.comp.text.tex recently.

With pdfLaTeX it is easy to add JavaScript at the document level. User defined JavaScript functions can be added as Action dictionaries (simple pdf objects) to the document. Then their names have to be mapped to the corresponding PDF object numbers in the /JavaScript name tree of the /Names dictionary in the PDF catalog using the pdfTeX primitive \pdfnames. Finally, the functions can be used within any PDF object that accepts an Action dictionary (links, widgets, pages, etc.). All this is documented in the PDF reference and in the JavaScript reference of Acrobat:

Here is a simple example which may serve as a starting point. Therein, doc-level JavaScript functions which accept arguments are used in the Action dictionaries of two links:

\documentclass{article}

\immediate\pdfobj{<<
/S/JavaScript/JS (someFunc=function(arg){app.alert('I say ' + arg);})
>>}
\edef\someFunc{\the\pdflastobj\space 0 R}

\immediate\pdfobj{<<
/S/JavaScript/JS (otherFunc=function(arg){app.alert('You say ' + arg);})
>>}
\edef\otherFunc{\the\pdflastobj\space 0 R}

\pdfnames{
/JavaScript <<
%name tree mapping doc. level JavaScript functions to object numbers
/Names [(someFunc) \someFunc (otherFunc) \otherFunc ]
/Limits [(otherFunc) (someFunc)] %first and last in alphab. order
>>
}

\begin{document}
\leavevmode
/A <<
/S/JavaScript
/JS (someFunc('Hello!'))
>>

\leavevmode
/A <<
/S/JavaScript
/JS (otherFunc('Good bye!'))
>>