Access iFrame contentWindow in Service Portal Client script

vinuret
Kilo Contributor

Hi there,

I am using an iframe in Service portal widget to load page from another application. I have a requirement to use window.postMessage() to post a message from the widget (client script) to the child window. From the client script I tried to access the content window using $window.frames[index].contentWindow but it does not seem to work.

Can someone please suggest me how to achieve this?

Thanks in advance,

Vinu

1 ACCEPTED SOLUTION

Oleg
Mega Sage

I'd recommend you to use $window.document.querySelector or $window.document.getElementById or $window.document.querySelectorAll to query iframe element, which content you need to access. For example, if your custom Service Portal page has only one iframe, you can use $window.document.querySelector("iframe").contentWindow to access the contentWindow. Alternatively, angular.element("iframe")[0].contentWindow should work too.

View solution in original post

5 REPLIES 5

Oleg
Mega Sage

I'd recommend you to use $window.document.querySelector or $window.document.getElementById or $window.document.querySelectorAll to query iframe element, which content you need to access. For example, if your custom Service Portal page has only one iframe, you can use $window.document.querySelector("iframe").contentWindow to access the contentWindow. Alternatively, angular.element("iframe")[0].contentWindow should work too.

vinuret
Kilo Contributor

Hi Oleg,

Than you for your quick response but unfortunately these steps do not seem to work.

My client script start like the below

function($scope, $window) {

     ....................

     ...................

}

Inside the script I tried to access the iframe window like below

a) $window.document.querySelector("iframe") gives null

b) angular.element("iframe")[0].contentWindow throws error as angular.element("iframe") gives null

 

vinuret
Kilo Contributor

Hi Oleg,

My bad, please ignore my response above. I am able to access the Iframe using the steps you mentioned above.

Thank you so much,

Vinu

You are welcome!