Difficulty replacing embeded iframe video with custom script..
-
Newblar here, and I’m try to replace an iframe I have on my front page with a script to load the video contents only when clicked to decrease page load time. I’ve been following the example from this site (http://www.labnol.org/internet/light-youtube-embeds/27941/). My embeded video link: “https://www.youtube.com/embed/BeQl6qHVq8I”
This is the html I inserted into a text widget on my front page:
<div class=”youtube-container”>
<div class=”youtube-player” id=”BeQl6qHVq8I”></div>
</div>I copied and pasted the script and CSS onto my site into the appropriate areas, but the video does not load any image.
my site: (http://new.renaissanceeuropeanspa.com/)I’ve been playing around with this for a couple hours now and it’s driving me insane trying to figure it out. Can someone show me the light on this?
~thanks in advance π
-
I copied and pasted the script and CSS onto my site into the appropriate areas,
It’s important for us to know where you pasted the script.
both css and the script i used went into the head. (now the site is http://www.renaissanceeuropeanspa.com).
What was the script? So that we know what to look for
Andrew, thanks for the reply. The example I am following is from (http://www.labnol.org/internet/light-youtube-embeds/27941/).
Script below:(function() { var v = document.getElementsByClassName("youtube-player"); for (var n = 0; n < v.length; n++) { var p = document.createElement("div"); p.innerHTML = labnolThumb(v[n].dataset.id); p.onclick = labnolIframe; v[n].appendChild(p); } })(); function labnolThumb(id) { return '<img class="youtube-thumb" src="//i.ytimg.com/vi/' + id + '/hqdefault.jpg"><div class="play-button"></div>'; } function labnolIframe() { var iframe = document.createElement("iframe"); iframe.setAttribute("src", "//www.youtube.com/embed/" + this.parentNode.dataset.id + "?autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&controls=0&showinfo=0"); iframe.setAttribute("frameborder", "0"); iframe.setAttribute("id", "youtube-iframe"); this.parentNode.replaceChild(iframe, this); }I posted this script inline into my <head>. I’ve tried several variants of this, modifying the script with my video ID, into and out of text widgets, and still no luck π Any ideas?
I don’t see the element with the class “youtube-player” on the Home page. You can search too here: view-source:http://www.renaissanceeuropeanspa.com/
I have it disabled right now. But when I was trying to congigure it, I inserted the HTML into a text widget, the CSS, and the script into the head with no results. I will try again right now..
nm I had to remove it.
Try putting that JavaScript at the bottom of your page, before the
</body>tag. If you put it at the top then it will run before the page has loaded.I tried that, and putting the script directly in the text widget with no luck :/
hey Andrew,
Finally got it working! The problem was my thumbnail images weren’t loading properly. Here is my version of the script I used (just changed the thumb string names from original script):<script> (function() { var v = document.getElementsByClassName("youtube-player"); for (var n = 0; n < v.length; n++) { var p = document.createElement("div"); p.innerHTML = labnolThumb(v[n].dataset.id); p.onclick = labnolIframe; v[n].appendChild(p); } })(); function labnolThumb(id) { return '<img class="youtube-thumb" src="https://img.youtube.com/vi/' + id + '/0.jpg"><div class="play-button"></div>'; } function labnolIframe() { var iframe = document.createElement("iframe"); iframe.setAttribute("src", "//www.youtube.com/embed/" + this.parentNode.dataset.id + "?autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&controls=0&showinfo=0"); iframe.setAttribute("frameborder", "0"); iframe.setAttribute("id", "youtube-iframe"); this.parentNode.replaceChild(iframe, this); } </script>Thanks for the help, I appreciate it.
The topic ‘Difficulty replacing embeded iframe video with custom script..’ is closed to new replies.