Last Updated:
September 27, 2021
by
Muskan Purohit
| Version: 14
| 1,337 views
| 0 followers
members are following updates on this item.
This article shows developers how they can access the data properties of the currently logged in user from inside a custom widget.
Note: Custom widgets are rendered on the page inside an iframe, thus functions of the parent document/window are not directly accessible by the logic inside your widget.
Tip: Since the iframe that contains the widget is rendered directly on the page from the same domain, there are no CORs issues and this document outlines how to modify your code to access the parent document/window.
Use the following code to access the data properties of the currently logged in user (Fig. 2.1):
/**
* @description Use this JavaScript inside your widget to access community data
*/
var userData = window.parent.Igloo.currentUser;
/** Your custom code here... */
console.log(userData);
(Fig. 2.1) Access user data within a custom widget.
The above script will output the following data for developers to work with (Fig 3.1):{id:"a123eee87-47ba-e511-80ca-000af7705322" namespace:"srowe8”, href: "/.profile/srowe8", name: "Steven Rowe", hasphoto: true}
hasphoto:true
href:"
/.profile/srowe8"
id: "a123eee87-47ba-e511-80ca-000af7705322"
name:"Steven Rowe"
namespace:"
srowe8
"
__proto__: Object
(Fig. 3.1) Console output of user data.
To learn more about how an iframe can interact with the parent document, and vice-versa, check out this MDN article