最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do you Defer Parsing of JavaScript for YouTube video on a WordPress site? - Stack Overflow

programmeradmin3浏览0评论

I just today realized that of my 1.2 MB site (per GTMetrix), 550k of it is a YouTube video.

My web site is a WordPress site, and the current video loads in an iframe.

I want my site to load faster... And not sure how to proceed. If I can make the YouTube video not load until clicked, and just have a thumbnail there instead, it seems like that would be the right play?

I just today realized that of my 1.2 MB site (per GTMetrix), 550k of it is a YouTube video.

My web site is a WordPress site, and the current video loads in an iframe.

I want my site to load faster... And not sure how to proceed. If I can make the YouTube video not load until clicked, and just have a thumbnail there instead, it seems like that would be the right play?

Share Improve this question edited Feb 14, 2017 at 12:46 Braiam 4,49611 gold badges49 silver badges83 bronze badges asked Aug 10, 2016 at 4:45 Michael GrippiMichael Grippi 2132 gold badges3 silver badges8 bronze badges 14
  • What is the best route to take to fix this? fix what? You haven't really stated what you want fixed, or what is broken - you've stated some facts that have no problem in them – Jaromanda X Commented Aug 10, 2016 at 4:47
  • Sorry. Edited to reflect my desires. – Michael Grippi Commented Aug 10, 2016 at 4:50
  • look into how to have the youtube video not auto play - in that case it shouldn't start downloading data until the user presses play – Jaromanda X Commented Aug 10, 2016 at 4:52
  • if you have &autoplay=1 in the url of the iframe, remove it, you'll be golden – Jaromanda X Commented Aug 10, 2016 at 4:53
  • 2 Take a look at: labnol.org/internet/light-youtube-embeds/27941 – Ahmad Essam Commented Aug 10, 2016 at 5:26
 |  Show 9 more comments

3 Answers 3

Reset to default 17

1. Replace “scr” with “data-src”

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

2. Add Javascript

function init() {
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
if(vidDefer[i].getAttribute('data-src')) {
vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
} } }
window.onload = init;

Now your site will load faster compared to past.

I have following this blog and it's working for me : http://www.codecanal.com/defer-parsing-javascript-youtube-videos/

You can achieve this with two simple steps......

  1. Update your embed code like flowing....
<iframe width="560" height="315" src="" data-src="generic_youtube_url" frameborder="0" allowfullscreen></iframe>
  1. Here is the java script part....
function init() { 
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
 if(vidDefer[i].getAttribute('data-src')) {
 vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
 } } } window.onload = init;

Reference For more details:

https://scottdeluzio.com/defer-parsing-javascript-youtube-videos/

https://varvy.com/pagespeed/defer-videos.html

P.S. I am personally using this and it works.

Thanks Sabbir H

The best and faster thing to do is to replace the video iframe by something like a link or an image, and load the iframe on click or another event.

Ahmed Essam linked you a solution, but if you don't want or don't know how to edit files on your wordpress installation, the Embed Video Thumbnail plugin does the job:

https://wordpress.org/plugins/embed-video-thumbnail/

发布评论

评论列表(0)

  1. 暂无评论