The function bound to (@load="myFunction"
) fires once when the iframe is created and once when it’s actually loaded.
Why does it fire when the iframe is created, and how to avoid it?
JavaScript
x
48
48
1
<template>
2
<transition name="modal">
3
<div v-if="webviewOpen">
4
<transition name="content" appear>
5
<div v-if="webviewOpen">
6
<transition name="iframe">
7
<iframe
8
v-show="showIframe"
9
:src="webviewUrl"
10
@load="iframeIsLoaded"
11
/>
12
</transition>
13
</div>
14
</transition>
15
</div>
16
</transition>
17
</template>
18
19
<script>
20
import { mapState } from 'vuex'
21
22
export default {
23
data () {
24
return {
25
showIframe: false
26
}
27
},
28
computed: {
29
mapState({
30
webviewOpen: state => state.webview.open,
31
webviewUrl: state => state.webview.url
32
})
33
},
34
watch: {
35
webviewOpen () {
36
setTimeout(() => {
37
this.showIframe = true
38
}, 1000)
39
}
40
},
41
methods: {
42
iframeIsLoaded () {
43
console.log('iframe loaded')
44
}
45
}
46
}
47
</script>
48
Advertisement
Answer
As @tao suggested something else was interefering, namely Nuxt Lazy Load package. So if anyone uses this package AND finds out iframes onload event mysteriously fires twice AND finds this thread:
Add iframes: false
in your nuxt.config.js
when importing the package inside the modules
section. Problem solved!