Apps.getSelf

  • Revision slug: Web/API/Apps.getSelf
  • Revision title: Apps.getSelf
  • Revision id: 469111
  • Created:
  • Creator: tregagnon
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

{{ non-standard_header() }}

Summary

Returns information about the current app, which means an installed app whose domain matches the domain of the calling app. You can use this to determine if an app is installed.

Note: Multiple apps per origin are not supported. To host several apps from one domain, set up a subdomain for each app. For example: myapp.mydomain.com, otherapp.mydomain.com, and so forth.

Syntax

var request = window.navigator.mozApps.getSelf();

Parameters

None.

Returns

getSelf() returns a DOMRequest object. When the success event is fired against the DOMRequest the DOMRequest.result field contains either an App object representing the current app, or a null if getSelf() was called outside of an app (meaning the app is not installed). Before the operation is finished, DOMRequest.result is null.

Note: Due to {{ bug(806597) }}, request.result incorrectly returns null when an app is running in Desktop Firefox.

If the call is not successful, an error event is fired against the DOMRequest and DOMRequest.error contains a DOMError object, which has information about the error.

Example

An example that shows how to use getSelf() with the DOMRequest.onsuccess and DOMRequest.onerror callback properties.

var request = window.navigator.mozApps.getSelf();
request.onsuccess = function() {
  if (request.result) {
    // Pull the name of the app out of the App object
    alert("Name of current app: " + request.result.manifest.name);
  } else {
    alert("Called from outside of an app");
  }
};
request.onerror = function() {
  // Display error name from the DOMError object
  alert("Error: " + request.error.name);
};

If the call is successful an App object is returned in the result property of the returned object. In this example this is request.result. If request.result is null, you know the app is not installed.

If your app changes its protocol between HTTP and HTTPS, then it is better to use navigator.mozApps.checkInstalled() to check if it is installed.

Revision Source

<p>{{ DomRef() }}</p>
<p>{{ non-standard_header() }}</p>
<h2 id="Summary">Summary</h2>
<p>Returns information about the current app, which means an installed app whose domain matches the domain of the calling app. You can use this to determine if an app is installed.</p>
<div class="note">
  <p><strong>Note:</strong> Multiple apps per origin are not supported. To host several apps from one domain, <a href="/en-US/docs/Web/Apps/Adding_a_subdomain">set up a subdomain for each app</a>. For example: <code>myapp.mydomain.com</code>, <code>otherapp.mydomain.com</code>, and so forth.</p>
</div>
<h2 id="Syntax">Syntax</h2>
<pre>
<code>var request = window.navigator.mozApps.getSelf()</code>;
</pre>
<h2 id="Parameters">Parameters</h2>
<p>None.</p>
<h2 id="Returns">Returns</h2>
<p><code>getSelf()</code> returns a <a href="/en-US/docs/Web/API/DOMRequest"><code>DOMRequest</code></a> object. When the <code>success</code> event is fired against the <code>DOMRequest</code> the <code>DOMRequest.result</code> field contains either an <a href="/en-US/docs/Web/API/App"><code>App</code> object</a> representing the current app, or a <code>null</code> if <code>getSelf()</code> was called outside of an app (meaning the app is not installed). Before the operation is finished, <code>DOMRequest.result</code> is <code>null</code>.</p>
<div class="note">
  <p><strong>Note:</strong> Due to {{ bug(806597) }}, <code>request.result</code> incorrectly returns <code>null</code> when an app is running in Desktop Firefox.</p>
</div>
<p>If the call is not successful, an <code>error</code> event is fired against the <code>DOMRequest</code> and <code>DOMRequest.error</code> contains a <a href="/en-US/docs/Web/Apps/JavaScript_API/Error_object"><code>DOMError</code></a> object, which has information about the error.</p>
<h2 id="Example">Example</h2>
<p>An example that shows how to use <code>getSelf()</code> with the <code>DOMRequest.onsuccess</code> and <code>DOMRequest.onerror</code> callback properties.</p>
<pre class="brush: js">
var request = window.navigator.mozApps.getSelf();
request.onsuccess = function() {
  if (request.result) {
    // Pull the name of the app out of the App object
    alert("Name of current app: " + request.result.manifest.name);
  } else {
    alert("Called from outside of an app");
  }
};
request.onerror = function() {
  // Display error name from the DOMError object
  alert("Error: " + request.error.name);
};</pre>
<p>If the call is successful an <a href="/en-US/docs/Web/API/App"><code>App</code></a> object is returned in the <code>result</code> property of the returned object. In this example this is <code>request.result</code>. If <code>request.result</code> is <code>null</code>, you know the app is not installed.</p>
<p>If your app changes its protocol between HTTP and HTTPS, then it is better to use <a href="/en-US/docs/Web/API/Apps.checkInstalled"><code>navigator.mozApps.checkInstalled()</code></a> to check if it is installed.</p>
<h2 id="Related_topics">Related topics</h2>
<ul>
  <li><a href="/en-US/docs/Web/Apps/JavaScript_API">Apps JavaScript API</a></li>
</ul>
Revert to this revision