How to check if an iframe is loaded in a Vue 3 component

Posted on October 16, 2022

Suppose you have an iframe in your component:

    <iframe src="/somepath.html"></iframe>

Here the code to listen for the loaded handler and change the class based on the iframe loaded status:

<script setup>
  import { ref, onMounted } from 'vue'
  const frame = ref()
  const loaded = ref(false)
  onMounted(() => {
    frame.value.onload = () => {
      loaded.value = true

    <iframe :class={loaded} ref="frame" src="/somepath.html"></iframe>

You can see that we need to refer the iframe usinf ref and using another reactive variable to change the current loaded status. onMouted is the initial trigger to attach the iframe native onload handler.