Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: runtime.getFrameId()
slug: Mozilla/Add-ons/WebExtensions/API/runtime/getFrameId
tags:
- API
- Add-ons
- Extensions
- Method
- Reference
- WebExtensions
- getBrowserInfo
- runtime
browser-compat: webextensions.api.runtime.getFrameId
---
{{AddonSidebar}}

Returns the frame ID of any window global or frame element when called from a content script or extension page, including background pages.

## Syntax

```js
var gettingInfo = browser.runtime.getFrameId()
```

### Parameters

- `target`
- : A {{glossary("WindowProxy")}} or a {{glossary("browsing context")}} container [Element](/en-US/docs/Web/API/Element) (iframe, frame, embed, or object) for the target frame.

### Return value

Returns the frame ID of the target frame, or -1 if the frame doesn't exist.

## Examples

This code recursivelly walks descendant frames and gets parent frame IDs.

```js
let parents = {};

function visit(win) {
let frameId = browser.runtime.getFrameId(win);
let parentId = browser.runtime.getFrameId(win.parent);
parents[frameId] = (win.parent != win) ? parentId : -1;

try {
let frameEl = browser.runtime.getFrameId(win.frameElement);
browser.test.assertEq(frameId, frameEl, "frameElement id correct");
} catch (e) {
// Can't access a cross-origin .frameElement.
}

for (let i = 0; i < win.frames.length; i++) {
visit(win.frames[i]);
}
}
visit(window);
```

{{WebExtExamples}}

## Browser compatibility

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't document Safari Technology Preview in compat data, I suggest we add something like this for now:

This method was proposed through the WebExtensions community group, implemented in Firefox 96 and will be available starting with Safari Technology Preview 142.

Copy link
Copy Markdown
Contributor Author

@rebloor rebloor Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compatibility data will include the Firefox release version. I'm not sure the information about how this method was proposed is relevant to compatibility. This would leave "Available in Safari Technology Preview 142." Presumably, that information would be superseded once it becomes part of Safari, so I'm reluctant to add on the basis we don't have a mechanism for removing this later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can add that note to compat data, that's also fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you review the browser-compat-data change?

{{Compat}}

> **Note:** Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ It also provides messaging APIs enabling you to:
- : Retrieves the [Window](/en-US/docs/Web/API/Window) object for the background page running inside the current extension.
- {{WebExtAPIRef("runtime.openOptionsPage()")}}
- : Opens your extension's [options page](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Options_pages).
- {{WebExtAPIRef("runtime.getFrameId()")}}
- : Gets the frame ID of any window global or frame element.
- {{WebExtAPIRef("runtime.getManifest()")}}
- : Gets the complete [manifest.json](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json) file, serialized as an object.
- {{WebExtAPIRef("runtime.getURL()")}}
Expand Down
2 changes: 2 additions & 0 deletions files/en-us/mozilla/firefox/releases/96/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ No notable changes.

## Changes for add-on developers

- Added {{WebExtAPIRef("runtime.getFrameId")}} that gets the frame ID of any window global or frame element from a content script ({{bug(1733104)}}).

## Older versions

{{Firefox_for_developers(95)}}