Stop all video youtube via javascript

I tried to use this javascript to stop all iframe youtube but it not work.


stopallvideoyoutube.hype.zip (37.8 KB)

Help me please, I dont know where I wrong
Thank you so much.

hi,

first: you should not nest iframes within a widget which itsself is an iframe too. just past the embedcode within a regular rectangle.

second: use this code provided at github unchanged. you’ve changed the queryselector from htmlelement to class …

third: give an unique id to every hypeelemnt that holds an iframe …

fourth: then run …
stopVideo(hypeDocument.getElementById('oneOfYourIds'))
stopVideo(hypeDocument.getElementById('nextId'))

good luck :slight_smile:

1 Like

If I use many rectangle, my app is very slow. I dont know why.
I dont want give unique ID for every iframe, I want to use class for all of iframe.
Thanks

then you’ll need an iteration within the function … as the current paramter is an element and not a collection

var yourCustomClassForVideos = 	'yourCustomClass';
var labeledIframes =  Array.prototype.slice.call( document.getElementsByClassName(yourCustomClassForVideos), 0 );//labeled elements as array
labeledIframes.forEach(function (item) {//iterate thru array
  var embed = item.querySelector('iframe');//get optional first embedded iframe within labeled hypeelement
 if(embed) embed.src = embed.src //if iframe exist this'll reload ...
})

Sorry , Can you create a Hype file demo?
I tested on Hype with multi iframe but it not work.

you’ll take and place a widget
set your embedcode

place the button
attach a behaviour that’ll excute above js

don’t forget to set class: yourCustomClass

should work :slight_smile:

1 Like