jquery - Single function for Wistia autoplay -


i'm relatively new jquery , have been trying create single function wistia video player script instead of stacking same on , over. pages applied have many 20 videos need specific urls play each.

original:

wistiajquery(document).ready(function () {     var url = window.location.href.tostring();      // autoplay video url 'www.my-site.com/a-name'     if (url.indexof('a-name') != -1) {     wistiajquery('.a-name').click();     $(".wistia-fancybox #fancybox-overlay").css("display", "block");     }      // autoplay video url 'www.my-site.com/b-name'     if (url.indexof('b-name') != -1) {     wistiajquery('.b-name').click();     $(".wistia-fancybox #fancybox-overlay").css("display", "block");     }      // autoplay video url 'www.my-site.com/c-name'     if (url.indexof('c-name') != -1) {     wistiajquery('.c-name').click();     $(".wistia-fancybox #fancybox-overlay").css("display", "block");     }  }); 

into like:

wistiajquery(document).ready(function () {     function playvideo(selector) {         $(selector).each(function () {             var url = window.location.href.tostring();             var el = $(this);             var vid = function () {                 if (url.indexof(el) != -1) {                     wistiajquery('.' + el).click();                     $(".wistia-fancybox #fancybox-overlay").css("display", "block");                 }             };             $(el).on('load', vid);         });     }     playvideo('a-name,b-name,c-name'); }); 

your code close, needs couple of changes go:

  • you need use jquery.each() instead of .each(). if $('a-name,b-name,c-name'), nothing because there no element tags <a-name>, <b-name>, or <c-name>. split string array, , use $.each( array, function(index, value) { ... }) instead. replace references el value.
  • there's no need define vid() function , call when element loaded. element loaded placing code in $(document).ready() (and not waiting in original code anyway).

in end, use this:

wistiajquery(document).ready(function () {      function playvideo(selector) {         $.each(selector.split(","), function (idx, obj) {             var url = window.location.href.tostring();             if (url.indexof(obj) != -1) {                 wistiajquery('.' + obj).click();                 $(".wistia-fancybox #fancybox-overlay").css("display", "block");             }         });     }      playvideo('a-name,b-name,c-name');  }); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -