Onload trigger from iframe

What I’m trying to do:
I Have an iframe which starts on an index.html file and then transitions to a goodbye.html file when the content has been finished.

i need to add an onload function callback to the iframe.

What I’ve tried and what’s not working:
I’ve tried to add an attribute to the iframe, as shown in the code snippet, however this yields the expected result of “ReferenceError: iframe_loaded is not defined”, how can I add my function to the page before trying to load the iframe?

Code Sample:

iframe = None
  
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Create an iframe element and set the src
    self.iframe = jQuery("<iframe width='100%' height='800px'>").attr("src","url").attr("onload", "iframe_loaded()")
    self.iframe.appendTo(get_dom_node(self.content_panel))

  
  def iframe_loaded(self):
    # Check if the iframe source ends with goodbye.html
    if self.iframe != None:
      if self.iframe.attr('src').endswith('goodbye.html'):
        # Run your code here
        print("Transition to goodbye.html")`` 

Just a note, that the url provided is not actually “url” as the snippet shows.

You don’t need to add the iframe loaded function to the page.

Something like this should work.


    self.iframe = jQuery("<iframe width='100%' height='800px'>").attr("src","url").on("load", self.iframe_loaded)


You’ll also likely need to add *js_event_args to the call signature for iframe_loaded since jquery events will be passed to the event handler as extra args.

1 Like

Apologies for my delayed response!

This is exactly what I needed!
Thank you.