admin管理员组文章数量:1335439
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?
<template>
<transition name="modal">
<div v-if="webviewOpen">
<transition name="content" appear>
<div v-if="webviewOpen">
<transition name="iframe">
<iframe
v-show="showIframe"
:src="webviewUrl"
@load="iframeIsLoaded"
/>
</transition>
</div>
</transition>
</div>
</transition>
</template>
<script>
import { mapState } from 'vuex'
export default {
data () {
return {
showIframe: false
}
},
puted: {
...mapState({
webviewOpen: state => state.webview.open,
webviewUrl: state => state.webview.url
})
},
watch: {
webviewOpen () {
setTimeout(() => {
this.showIframe = true
}, 1000)
}
},
methods: {
iframeIsLoaded () {
console.log('iframe loaded')
}
}
}
</script>
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?
<template>
<transition name="modal">
<div v-if="webviewOpen">
<transition name="content" appear>
<div v-if="webviewOpen">
<transition name="iframe">
<iframe
v-show="showIframe"
:src="webviewUrl"
@load="iframeIsLoaded"
/>
</transition>
</div>
</transition>
</div>
</transition>
</template>
<script>
import { mapState } from 'vuex'
export default {
data () {
return {
showIframe: false
}
},
puted: {
...mapState({
webviewOpen: state => state.webview.open,
webviewUrl: state => state.webview.url
})
},
watch: {
webviewOpen () {
setTimeout(() => {
this.showIframe = true
}, 1000)
}
},
methods: {
iframeIsLoaded () {
console.log('iframe loaded')
}
}
}
</script>
Share
Improve this question
edited Apr 27, 2021 at 20:10
drake035
asked Apr 27, 2021 at 18:43
drake035drake035
2,89749 gold badges137 silver badges258 bronze badges
6
- We need more code. The problem is elsewhere. This code loads only once for me. – tauzN Commented Apr 27, 2021 at 19:48
-
1
Still only
console.log
s once for me – tauzN Commented Apr 27, 2021 at 20:26 - stackoverflow./a/15880489/871404 clearly this problem is faced by many people. I tried this answer and it doesn't work for me. – drake035 Commented Apr 28, 2021 at 12:13
-
@drake035, It is clearly not happening in latest Chrome, using Vue
2.6.12
. Provide a minimal reproducible example specifying at least one browser in which it happens. Without a way to reproduce, nobody can provide a fix. Most likely something else (layout structural directives e.gv-if
,v-for
) is causing your<iframe>
to be removed from DOM and then added again. What you have posted so far cannot be tested. – tao Commented May 5, 2021 at 2:59 -
1
A wild guess here is your ponent starts with a truthy value for
webviewOpen
, which is swiftly (possibly inmounted()
) changed to a falsy value. This could cause the<iframe>
to fire@load
before the execution ofmounted()
, if thesrc
is falsy at that point - as it doesn't take any time to load. Obviously, the@load
will fire again when you switchwebviewOpen
to a truthy value later on. This is pure speculation, since you haven't provided a way to repro, hence test. But if the assumption is true, initializingwebviewOpen
as falsy would fix it. – tao Commented May 5, 2021 at 12:18
3 Answers
Reset to default 3It seems it may be a web kit issue with firing twice ( safari/chrome ) as it fires when added to DOM (v-if on parent) and when the content is loaded. It may help to add .once modifier to the @load.once="myFunction()"
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!
We know from your linked answer that Chrome shows this issue unless you attach the listener after the iframe
is appended to the DOM. To do this, we could take advantage of Vue's lifecycle hooks. We want it to happen after the iframe
is added to the DOM, but before it has a chance to load, so we'll use the updated
hook.
I don't experience the problem in any of my browsers, so I unfortunately can't really test it for you. Test for yourself and see if something like this fixes it for you:
<template>
<label for="show">Show iFrame</label>
<input id="show" type="checkbox" v-model="webviewOpen">
<div v-if="webviewOpen">
<iframe
src="https://testwebsite./"
@load="iframeLoadHelper"
frameborder="0"
></iframe>
</div>
</template>
<script>
export default {
name: 'App',
data() {
return {
webviewOpen: false,
iframeReady: false
};
},
methods: {
// Helper method, just to keep 'if' outside of the logic of your potentially
// plex @load method
iframeLoadHelper() {
if (this.iframeReady) return this.iframeLoaded();
else return; // do nothing
},
// The real load method
iframeLoaded() {
console.log('iframe loaded');
}
},
updated() {
console.log('changing ready-state');
this.iframeReady = this.webviewOpen;
}
};
</script>
<style>
:root { font-family: sans-serif; }
</style>
本文标签: javascriptIframe load event fires twiceStack Overflow
版权声明:本文标题:javascript - Iframe load event fires twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340271a2456460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论