video_privacy_enhancer_jquery.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. $(document).ready(function() {
  2. // NOTE WELL: This is wrapped in a `$(document).ready(function() {...})` function (i.e., a "Document Ready" function that starts a section that won't load until the rest of the document/page has loaded (i.e., is "ready")). HOWEVER, this makes it so that jQuery functions from different .js files here are not able to see and talk to functions within this Document Ready function. Within a Document Ready function, functions and variables lose global scope. See http://stackoverflow.com/a/6547906 for more information.
  3. ///////////////////////////
  4. // SETTINGS
  5. ///////////////////////////
  6. // NOTE WELL: It's expected to use the code 'VIDEO_ID_GOES_HERE' in the URLs below where the video ID should go. This phrase will be replaced by the video id below.
  7. // NOTE WELL: The URLS below should have 'http://' or 'https://' in front of them:
  8. var dictionary_of_embed_urls_for_different_services = {
  9. "youtube" : 'https://www.youtube-nocookie.com/embed/VIDEO_ID_GOES_HERE',
  10. "vimeo" : 'https://player.vimeo.com/video/VIDEO_ID_GOES_HERE'
  11. };
  12. ///////////////////////////
  13. // END OF SETTINGS
  14. ///////////////////////////
  15. // Whenever a link for an image created with the video_privacy_enhancer plugin is clicked, transform it into a video embed:
  16. $('body').on("click",'img.video-embed-dummy-image',function(event) // Whenever an image with the class "video-embed-dummy-image" is clicked...
  17. {
  18. // '$(this)' below refers to the image that's been clicked. Before we go down into the function below, where the definition/scope of '$(this)' will change, we'll set it in a variable so that we can refer to it within the function below.
  19. var image_that_was_clicked = $(this);
  20. var video_id = $(this).attr('id'); // This assumes that the img ID attribute is set to the (e.g., youtube, Vimeo, etc.) video id.
  21. var service_name = $(this).attr('embed-service'); // This assumes that the 'embed-service' attribute is set to a name (e.g., youtube, vimeo), and that that name is in the dictionary below.
  22. var src_for_this_video = dictionary_of_embed_urls_for_different_services[service_name].replace("VIDEO_ID_GOES_HERE", video_id);
  23. // Fade out the image, and replace it with the iframe embed code for the actual video:
  24. $(this).fadeOut(250, function() {
  25. image_that_was_clicked.replaceWith('<iframe class="embedded_privacy_video" src="' + src_for_this_video + '" frameborder="0" allowfullscreen></iframe>') // This will automatically show again.
  26. }); // End fadeOut.
  27. });
  28. }) // End Document Ready wrapper.