Adding JavaScript to a Web applications is a great way to add features that mimic those found client/server applications without sacrificing all of the benefits of Web deployment. Oracle HTML DB includes multiple built-in interfaces especially designed for adding JavaScript.
Remember that JavaScript is not appropriate for data intensive validations. For example, to verify that a name is contained within a large database table, you would need to pull down every record to the client, creating a huge HTML document. In general, complex operations are much better suited for server-side HTML DB validations instead of JavaScript.
Topics:
When you reference an item, the best approach is to reference by ID. If you view the HTML source of an Oracle HTML DB page in a Web browser, you would notice that all items have an ID attribute. This ID corresponds to the name of the item, not the item label. For example, if you create an item with the name P1_FIRST_NAME
and a label of First Name
, the ID will be P1_FIRST_NAME.
Knowing the item ID enables you to use the JavaScript function getElementById
to get and set item attributes and values. The following example demonstrates how to reference an item by ID and display its value in an alert box.
<script language="JavaScript1.1" type="text/javascript"> function firstName(){ alert('First Name is ' + document.getElementById('P1_FIRST_NAME').value ); } // or a more generic version would be function displayValue(id){ alert('The Value is ' + document.getElementById(id).value ); } </script> // Then add the following to the "Form Element Attributes" Attribute of the item: onChange="javascript:displayValue('P1_FIRST_NAME');"
There are two primary places to include JavaScript functions:
In the HTML Header attribute of the page
In a .js file in the page template
See Also: "Text with JavaScript Escaped Single Quotes" for information about referencing a shortcut inside of a JavaScript literal string |
One way to include JavaScript into your application is to add it to the HTML Header attribute of the page. This is a good approach for functions that are very specific to a page as well as a convenient way to test a function before you include it in the .js
file.
You can add JavaScript functions to a page by simply entering the code into the HTML Header attribute of the Page Attributes page. For example, adding the following would make the test
function accessible from anywhere on the current page:
<script language="JavaScript1.1" type="text/javascript"> function test(){ alert('This is a test.'); } </script>
In Oracle HTML DB you can reference a .js
file in the page template. This approach makes all the JavaScript in that file accessible to the application. This is the most efficient approach since a .js
file loads on the first page view of your application and is then cached by the browser.
The following demonstrates how to include a .js file in the header section of a page template:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>#TITLE#</title> #HEAD# <script src="#APP_IMAGES#custom.js" type="text/javascript"></script> </head> <body #ONLOAD#>#FORM_OPEN#
Calling a JavaScript from a button is a great way to confirm a request. Oracle HTML DB uses this technique for the delete operation of most objects. For example, when you delete a button, a JavaScript message appears asking you to confirm your request. Consider the following example:
<script language="JavaScript1.1" type="text/javascript"> function deleteConfirm(msg) { var confDel = msg; if(confDel ==null) confDel= confirm("Would you like to perform this delete action?"); else confDel= confirm(msg); if (confDel== true) doSubmit('Delete'); } </script>
This example creates a function to confirm a delete action and then calls that function from a button. Note that the function optionally submits the page and sets the value of the internal variable :REQUEST
to Delete
, thus performing the delete using a process that conditionally executes based on the value of request.
Note that when you create the button you would need to select Action Redirect to URL without submitting page. Then, you would specify a URL target such as the following:
javascript:confirmDelete('Would you like to perform this delete action?');