如何在iframe之后自动加载javascript

i have iframe embed code of youtube video and i want to auto load javascript after each iframe. i using wordpress.

I want to create this - when the user open any post/page in my wordpress website, if the post/page contain any iframe then javascript auto load after each iframe.

this is javascript which i want to load after iframe.

<script src="https://apis.google.com/js/platform.js"></script>
<script>
function onYtEvent(payload) {
if (payload.eventType == 'subscribe') {
  // Add code to handle subscribe event.
} else if (payload.eventType == 'unsubscribe') {
  // Add code to handle unsubscribe event.
}
if (window.console) { // for debugging only
  window.console.log('YT event: ', payload);
}
}
</script>

<div class="g-ytsubscribe" data-channelid="UC_Lmg76m-DMN_EVURz_G9zw" data-layout="full" data-count="hidden" data-onytevent="onYtEvent"></div>

this is iframe

<iframe width="560" height="315" src="https://www.youtube.com/embed/blablabla" frameborder="0" allowfullscreen></iframe>

i wanna create like this

2个回答

use the onload function for auto loading the javascript

<iframe onload="onYtevent();" width="560" height="315" src="https://www.youtube.com/embed/blablabla"     frameborder="0" allowfullscreen></iframe>



你必须使用 .load()</ code> jquery函数。</ p>

在iframe加载后触发。</ p>

代码:</ p>

  $('iframe')。load(function(){

//这里是你的代码
});
</ code> </ pre>
</ div>

展开原文

原文

You have to use .load() jquery function.

It's fired after iframe has been loaded.

That's code:

$('iframe').load(function() {
   // here your code
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐