Working with iframes
iframes on your website can use the commands provided by the CMP API. They can do this by making calls to the parent's (or ancestor's) frame that the CMP is loaded on, sending the commands as a message with the postMessage() function.
The frame to make the call to can be determined by the ancestor by looking for a.frames["__tcfapiLocator"] child.

Send a command

The message to send should have the below form where the __tcfapiCall object holds the information about the command to execute. command and parameter are the same as the parameters passed to the __tcfapi() function, and callId est un ID unique à déterminer.
__tcfapiCall format
Example
1
{
2
__tcfapiCall: {
3
command: *command*, // name of the command to execute
4
version: 2, // version of the TCF specification
5
parameter: *parameter*, // parameter to be passed to the command function
6
callId: *uniqueId* // unique call ID
7
}
8
}
Copied!
1
this.callId = this.callId || 0;
2
var message = {
3
__tcfapiCall: {
4
command: 'getTCData',
5
version: 2,
6
parameter: [1,2,53],
7
callId: ++this.callId
8
}
9
};
10
window.top.postMessage(message, '*');
Copied!

Retrieve the command result

Before sending messages, the iframe must define a JavaScript function to execute when a message is sent back. To do this, it must add a listener for the event message with the window.addEventListener() function. By doing this, it will be able to receive and process the command result in __tcfapiReturn.
Thewindow.addEventListener()function is different from the addEventListener command, which is included in the CMP API.
Example
1
function processMessage(event) {
2
if (event && event.data && event.data.__tcfapiReturn && event.data.__tcfapiReturn.success) {
3
console.log(event.data.__tcfapiReturn.returnValue);
4
}
5
}
6
window.addEventListener('message', processMessage);
Copied!
When a command is sent, the returned message (event.data) holds the __tcfapiReturn object with the command result. returnValue and success are the same as the parameters passed to the callback of the __tcfapi() function, and callId is the ID from the origin call (__tcfapiCall).
__tcfapiReturn format
Example
1
{
2
__tcfapiReturn: {
3
returnValue: *returnValue*, // result of the command
4
success: *boolean*, // true if the call to the command was successful
5
callId: *uniqueId* // call ID sent in the __tcfapiCall
6
}
7
}
Copied!
1
function processMessage(event) {
2
if (event && event.data && event.data.__tcfapiReturn && event.data.__tcfapiReturn.success) {
3
var tcData = event.data.__tcfapiReturn.returnValue;
4
if (!tcData.gdprApplies) {
5
console.log("GDPR doesn't apply to user");
6
}
7
console.log("TC String : ", tcData.tcString);
8
}
9
}
10
window.addEventListener('message', processMessage);
11
12
this.callId = this.callId || 0;
13
var message = {
14
__tcfapiCall: {
15
command: 'getTCData',
16
version: 2,
17
parameter: [1, 2, 53],
18
callId: ++this.callId
19
}
20
};
21
window.top.postMessage(message, '*');
Copied!
Last modified 1mo ago