utils object
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:
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:
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. Parameters:
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:
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?
|
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?
|
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?
|
External links:
User Contributed Comments