Mobile/Fennec/CodeSnippets: Difference between revisions

Jump to navigation Jump to search
m
Line 3: Line 3:
Each snippet normally includes some code to run at initialization. These are best run using a load listener. These snippets assume they are run in the context of a browser window.
Each snippet normally includes some code to run at initialization. These are best run using a load listener. These snippets assume they are run in the context of a browser window.


== Load Listener ==
== Load Listener ==You can't usually just run UI code as global code in your JavaScript. You need to wait for the XUL window to finish loading before trying to access any XUL elements.<pre>window.addEventListener("load", startup, true);function startup() { // access UI elements here}</pre>== Access the Browser ==Fennec has several JavaScript objects you should use to access the browser and the browser UI. The main objects are <code>Browser</code> and <code>Tab</code>. They have the following APIs:<pre>Browser-------  property browsers    gets an array of all XUL browsers.  property selectedBrowser    gets the currently active XUL browser.  function getTabAtIndex(index)    returns Tab or null.  function getTabFromContent(content)    returns Tab or null.  function addTab(uri, bringFront)    creates a new Tab with the given URI. returns the new Tab.  function closeTab(tab)    closes the given Tab.  property selectedTab    sets/gets the selected Tab.Tab------  property browser    gets the XUL browser for the Ta
You can't usually just run UI code as global code in your JavaScript. You need to wait for the XUL window to finish loading before trying to access any XUL elements.
 
<pre>
window.addEventListener("load", startup, false);
 
function startup() {
  // access UI elements here
}
</pre>
 
== Access the Browser ==Fennec has several JavaScript objects you should use to access the browser and the browser UI. The main objects are <code>Browser</code> and <code>Tab</code>. They have the following APIs:<pre>Browser-------  property browsers    gets an array of all XUL browsers.  property selectedBrowser    gets the currently active XUL browser.  function getTabAtIndex(index)    returns Tab or null.  function getTabFromContent(content)    returns Tab or null.  function addTab(uri, bringFront)    creates a new Tab with the given URI. returns the new Tab.  function closeTab(tab)    closes the given Tab.  property selectedTab    sets/gets the selected Tab.Tab------  property browser    gets the XUL browser for the Tab.  property content    gets the XUL element holding the Tab thumbnail.  function isLoading()    returns whether the Tab is busy loading content.</pre>


== Listening for Browser Events ==
== Listening for Browser Events ==
2

edits

Navigation menu