Using the extension in your app

To have access to the JavaScript extension in your BlackBerry WebWorks application, place the JAR file containing the JavaScript extension in the ext folder in your project directory. Your project directory is the one that contains the config.xml and index.html files.

To use the JavaScript extension in your application, call the extension defined in the loadFeature() method of the class implementing the WidgetExtension interface to access the extension's Scriptable object, followed by a dot and the function name.

Use the JavaScript extension in a BlackBerry WebWorks app

Before you begin: Make sure that you created a subfolder named ext in your BlackBerry WebWorks app project, and that you have imported the .jar file that contains your JavaScript extension into the ext folder.
  1. In your index.htm file, add the metadata and title in the head tag. The title is the name of the class that extends Scriptable.
    <html>
    <head>
        <meta name="viewport" content="width=device-width,target-densitydpi=device-dpi,
            user-scalable=no,initial-scale=1.0" />
        <title>Alert Sample</title>
    </head>
    </html>
  2. In the body tag, add UI controls such as a text input field and a submit button. In this code sample, the number the BlackBerry device user types in the text input field specifies the duration of a notification, the BlackBerry device vibration, in milliseconds. The default duration is 1000 ms if this field is left blank. The submit button starts a vibration by invoking the vibrateAlert() function described in step 3.
    <html>
    <head>
        <meta name="viewport" content="width=device-width,target-densitydpi=device-dpi,
            user-scalable=no,initial-scale=1.0" />
        <title>Alert Sample</title>
    </head>
    
    <body>
        Duration: <input type="text"  id="txtLength" value="1000" /> (ms)
        <input type="submit" value="Vibrate me!" onClick="vibrateAlert()" />
    </body>
    </html>
  3. In the head tag, in a script tag, add JavaScriptcode to invoke the JavaScript extension method, passing in the duration of the vibration, to start the vibration when the user clicks the submit button.
    <head>
        <meta name="viewport" content="width=device-width,target-densitydpi=device-dpi,
            user-scalable=no,initial-scale=1.0" />
        <title>Alert Sample</title>
        <script type="text/JavaScript">
        function vibrateAlert()
        {
            if (sample.alert.vibrateSupported)
            {
                var ele = document.getElementById("txtLength");
                var iDuration = parseInt(ele.value);
                sample.alert.vibrate(iDuration);
            }
            else
            {
                alert('sample.alert.vibrate is not supported');
            }
        }
        </script>    
    </head>
  4. Grant your app permission to use the functionality in the extension by adding its feature to the config.xml file. Open the config.xml file and add the feature element.
    <?xml version="1.0" encoding="UTF-8"?>
    <widget xmlns="http://www.w3.org/ns/widgets" 
            xmlns:rim="http://www.blackberry.com/ns/widgets" 
            version="1.0.0.0">
       <name>MyExtensionTest</name>
       <feature  id="sample.alert" required="true" version="1.0.0"/>
       <content src="index.html"/>
    </widget>

Code sample: Using the Java extension in a BlackBerry WebWorks application

<html>
<head>
    <meta name="viewport" content="width=device-width,
        target-densitydpi=device-dpi,user-scalable=no,initial-scale=1.0" />
    <title>Alert Sample</title>
    <script type="text/JavaScript">
    function vibrateAlert()
    {
        if (sample.alert.vibrateSupported)
        {
            var ele = document.getElementById("txtLength");
            var iDuration = parseInt(ele.value);
            sample.alert.vibrate(iDuration);
        }
        else
        {
            alert('sample.alert.vibrate is not supported');
        }
    }
    </script>    
</head>

<body>
    Duration: <input type="text"  id="txtLength" value="1000" /> (ms)
    <input type="submit" value="Vibrate me!" onClick="vibrateAlert()" />
</body>
</html>

Last modified: 2013-08-14