2

i am dynamically creating and adding an iframe like this -

$A.createComponent(
      'iframe',
      {
        'aura:id': 'iframe',
        src: iframeUrl,
      },
      function(createdIframe, status) {
         if (status === 'SUCCESS') {
          var body = component.get('v.body');
          body.push(createdIframe);
          component.set('v.body', body);
         }
      }
)

The iframe is getting added by this. Now i want to add an onload event handler to the iframe to detect when the iframe does not load for the given iframeUrl.

I tried these 2 approaches -

  1. I added onload to the list of params passed to createComponent -

    $A.createComponent(
          'iframe',
          {
            'aura:id': 'iframe',
            src: iframeUrl,
            onload: "alert('iframe loaded')"
          },
    
  2. I added addEventListener to the createdIframe.

    createdIframe.addEventHandler('load', function() {
                alert('iframe loaded');
              });
    

But in both cases, the alert wasn't called. Not sure what I am missing here. Can you please help me with finding out when is the iframe onload called.

3
  • did you try onLoad:component.getReference('c.handleiframeonload'); then you can create new function with name handleiframeonload : function(cmp,evt,hel){}
    – sdandamud1
    Dec 23 '19 at 21:14
  • @sdandamud1 worked with onload:component.getReference('c.handleiframeonload'); Please add as an answer so i can accept and close. Thanks!
    – ghostCoder
    Dec 24 '19 at 7:26
  • added as answer
    – sdandamud1
    Dec 24 '19 at 12:48
1

you should bind event dynamically with component.getreference. and can reference another function in the controller. your code should be

$A.createComponent(
  'iframe',
  {
    'aura:id': 'iframe',
    src: iframeUrl,
    onload:component.getReference('c.handleiframeonload');
  },);

handleiframeonload : function(cmp,evt,hel){
// write your onload logic here.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.