Try OpenEdge Now
skip to main content
Developing WebSpeed Applications
Using JavaScript with WebSpeed : Using JavaScript source files
 

Using JavaScript source files

If you develop JavaScript routines that are long and that are common to a number of applications, you may want to keep them in separate JavaScript source (.js) files.
When deploying JavaScript source files, you must put them in the Web server's root directory or in a subdirectory of the Web server's root directory. You can then reference the file using the HTML SRC attribute. For example, the following calls myscript.js, which is in jsscripts a subdirectory of the Web server's root directory:
<SCRIPT LANGUAGE="JavaScript" SRC="/jsscripts/myscript.js">
You can also create references to JavaScript source files within Embedded SpeedScript. However, you must be careful to specify the complete <SCRIPT> tag expression and to escape the expression using back tics. For example, the following code snippet will not run the JavaScript file:
<SCRIPT LANGUAGE="SpeedScript">
  DEFINE VARIABLE myVar AS CHARACTER NO-UNDO.
  myVar = "/jsscripts/myscript.js".
</SCRIPT>

<H3>Trying to run JavaScript ...</H3>
<SCRIPT LANGUAGE="JavaScript" SRC="`myVar`">
</SCRIPT>

<H3>JavaScript did not run...</H3>
The problem is fixed when you assign the complete <SCRIPT> tag expression to the variable and enclose it in back tics, as shown:
<SCRIPT LANGUAGE="SpeedScript">
  DEFINE VARIABLE myVar AS CHARACTER NO-UNDO.
  myVar = '<SCRIPT LANGUAGE="JavaScript"
    SRC="/jsscripts/myscript.js"></SCRIPT>'.</SCRIPT>
<H3>Now running JavaScript ...</H3>
`myvar`
<H3>JavaScript done ...</H3>