Advertisement
nicolaslagios

Simple Lazy Load Videos - wordpress plugin - add href to anchor with javascript

Aug 1st, 2023 (edited)
1,450
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.24 KB | Source Code | 1 0
  1. //Simple Lazy Load Videos - wordpress plugin - add href to anchor with javascript by Nicolas Lagios / Rocket Path P.C.
  2. document.addEventListener('DOMContentLoaded', function() {
  3.   // Εύρεση Elements με κλάση "sllv-video"
  4.   const sllvVideos = document.querySelectorAll('.sllv-video');
  5.  
  6.   // Loop για όλα τα elements που βρέθηκαν με "sllv-video" και προσθήκη href στο a element
  7.   sllvVideos.forEach(function(video) {
  8.     // Video id από "data-video"
  9.     const videoId = video.getAttribute('data-video');
  10.  
  11.     // Έλεγχος αν το videoId δεν είναι κενό
  12.     if (videoId) {
  13.       // Δημιουργία του URL του βίντεο με βάση το videoId
  14.       const videoUrl = `https://www.youtube.com/watch?v=${videoId}`;
  15.  
  16.       // Εύρεση του anchor element
  17.       const anchorTag = video.querySelector('.sllv-video__link');
  18.  
  19.       // Έλεγχος αν υπάρχει το anchor και αν υποστηρίζει τη μέθοδο setAttribute
  20.       if (anchorTag && typeof anchorTag.setAttribute === 'function') {
  21.         // Προσθήκη href με το URL του βίντεο
  22.         anchorTag.setAttribute('href', videoUrl);
  23.       }
  24.     }
  25.   });
  26. });
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement