> For the complete documentation index, see [llms.txt](https://docs.exoclick.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.exoclick.com/advertisers/zh/guang-gao-xi-lie/iframe-events.md).

# Iframe 事件

广告主可以跟踪其广告中的事件。这些事件在客户端浏览器中借助 JavaScript 直接以程序方式管理。

## 事件

### creativeDisplayed

{% hint style="info" %}
这仅适用于 **桌面端全页插页广告** 和 **移动端全页插页广告** (*DFPI/MFPI*) 广告格式活动，使用 **iframe URL** 变体。
{% endhint %}

当广告展示给用户时，会向 iframe 中发送一条名为 `creativeDisplayed`.

你可以通过使用下面的脚本在你的 iframe 中跟踪此事件；该脚本会添加一个事件监听器，在广告展示给用户时捕获发送的消息。

```html
<script>
        function readMessage(event) {
            console.log("readMessage 事件");
            console.log("来源: " + event.origin);
            console.log("数据: " + event.data);
        }

        if (window.addEventListener) {
            // 适用于符合标准的网页浏览器
            window.addEventListener("message", readMessage, false);
        } else {
            window.attachEvent("onmessage", readMessage);
        }
</script>
```

## 示例 - 全页插页广告

```html
<!DOCTYPE html>
<html>
<head>
    <!-- 事件部分 -->
    <script>
        function readMessage(event) {
            console.log("readMessage 事件");
            console.log("来源: " + event.origin);
            console.log("数据: " + event.data);
            var message = "我们收到消息：'" + event.data + "'，来自 '" + event.origin + "'（我们现在位于广告主 iframe 中）";
            console.log(message);
            document.getElementById("received-message").innerHTML = message;
            // 即：广告主想要更新某个元素
        }
        if (window.addEventListener) {
            // 适用于符合标准的网页浏览器
            window.addEventListener("message", readMessage, false);
        } else {
            window.attachEvent("onmessage", readMessage);
        }
    </script>
    <!-- 事件部分结束 -->
</head>
<body>
    <h1>我的测试 iframe 标题</h1>
    <p>我的测试 iframe 段落</p>
    <p id="received-message">等待消息</p>
</body>
</html>
```

<figure><img src="https://1346658132-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi4Is92LUeIhwL8XSbGLq%2Fuploads%2Fi5JLh6QPqPkUvOLOHp8C%2Fexoclick_interstitial_iframe_creativeDisplayed.gif?alt=media&amp;token=d6f5f9a9-5944-4365-a1eb-3e6bfdca2520" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.exoclick.com/advertisers/zh/guang-gao-xi-lie/iframe-events.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `build a script that syncs our docs to a CMS` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
