You are here: Reference > JavaScript > client-side > browser > objects > utils

utils object

Browser support:
Implements useful features for the XPConnect (Cross Platform Connect) technology.

Syntax:

Properties that reference the object:
Components.utils

Possible members:

Methods:
evalInSandbox (code, Sandbox)
Executes a piece of JavaScript code in a Sandbox object.
See Example 1 to see how this method works in practice.

Parameters:

code
Required. String that specifies the code to execute.
Sandbox
Required. Reference to the Sandbox object that the code needs to be executed in. Use the Sandbox method to create a Sandbox object

Return value:

The result of the code.
forceGC ( )
Allows to force a garbage collection (automatic memory management) cycle.

Return value:

This method has no return value.
getWeakReference ( )
Returns an instance of the nsIWeakReference interface.

Return value:

An instance of the nsIWeakReference interface.
import (URL[, scope])
Includes a JavaScript code module into the current script. You can define a scope for the contents of the imported file.

Parameters:

URL
Required. String that specifies the URL of the JavaScript code module to be loaded.
scope
Optional. A reference to an object that will contain the imported code. If not set, the code will be imported into the global object.

Return value:

This method has no return value.
reportError (object)
Reports an error to the Error Console.
The nsIConsoleService object can also be used for reporting messages to the Error Console. See Example 2 and 3 for details.
To send messages to the Console window instead of the Error Console, use the dump method.
  • In Internet Explorer, use the Debug object to send messages to the output window of an opened script debugger.
  • In Opera, use the postError method to print messages to the Error Console.

Parameters:

object
Required. The object to report. If it is an Error object, the message of the error will be dumped, else the object will be converted to a string for reporting.

Return value:

This method has no return value.
Sandbox (URL)
Creates a Sandbox object that can be used as an executing environment for the evalInSandbox method.
See Example 1 to see how this method works in practice.

Parameters:

URL
Required. String that specifies the URL of the Sandbox object. This URL specifies the domain of the executing environment. Some JavaScript methods (e.g. XMLHttpRequest.open) require the referred URL to be located in the same domain as the executing environment.

Return value:

Returns the newly created Sandbox object.

Example HTML code 1:

This example demonstrates the use of the SandBox method:
<head>
    <script type="text/javascript">
        function TestInSandbox () {
            if (window.Components) {
                    // UniversalXPConnect privilege is required in Firefox
                try {
                    if (window.netscape && netscape.security) { // Firefox
                        netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
                    }
                }
                catch (e) {
                    alert ("UniversalXPConnect privilege is required for this operation!");
                    return;
                }

                var sandBox = window.Components.utils.Sandbox ("http://help.dottoro.com");
                sandBox.x = 3;
                var result = window.Components.utils.evalInSandbox ("y = 2 * x; x + y", sandBox);
                alert (result);
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body>
    <button onclick="TestInSandbox ();">Test addition in a Sandbox</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example prints a message to the Error Console:
<head>
    <script type="text/javascript">
        if (window.Components) {
                // UniversalXPConnect privilege is required in Firefox
            try {
                if (window.netscape && netscape.security) { // Firefox
                    netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
                }
            }
            catch (e) {
                alert ("UniversalXPConnect privilege is required for this operation!");
                return;
            }

            Components.utils.reportError ("An error message for the Error Console.");
        }
        else {
            alert ("Your browser does not support this example!");
        }
    </script>
</head>
Did you find this example helpful? yes no

Example HTML code 3:

Another solution to print a message to the Error Console:
<head>
    <script type="text/javascript">
        if (window.Components) {
                // UniversalXPConnect privilege is required in Firefox
            try {
                if (window.netscape && netscape.security) { // Firefox
                    netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
                }
            }
            catch (e) {
                alert ("UniversalXPConnect privilege is required for this operation!");
                return;
            }

            var console = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
            console.logStringMessage("A message for the Error Console.");
        }
        else {
            alert ("Your browser does not support this example!");
        }
    </script>
</head>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content