I have the following in my module.
drupal_add_js(
drupal_get_path('module', 'responsive_videos') . '/js/fluidvids.js',
array(
'type' => 'file',
'scope' => 'footer',
'weight' => 10,
)
);
For some reason, when this module is enabled, jQuery and jQuery Once get removed from the head and the JavaScript console logs many "Uncaught ReferenceError: jQuery is not defined". Can anyone clarify why this is happening, and how to fix it?
Here is the info file, responsive_videos.info.
name = Responsive Video
description = Creates a fluid responsive video from YouTube or Vimeo embedded iframes.
core = 7.x
And here is the module file, responsive_video.module.
<?php
/**
* @file
* Makes YouTube and Vimeo embedded iframe videos responsive.
*/
drupal_add_js(
drupal_get_path('module', 'responsive_video') . '/js/fluidvids.js',
array('scope' => 'footer')
);
The JavaScript file being added does not leverage jQuery, so I don't quite understand how or if it might interfere with jQuery being added to the page, but here is the source of the "fluidvids.js" file.
// http://toddmotto.com/fluid-and-responsive-youtube-and-vimeo-videos-with-fluidvids-js/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.fluidvids = factory();
}
})(this, function () {
'use strict';
var fluidvids = {
selector: ['iframe'],
players: ['www.youtube.com', 'player.vimeo.com']
};
var css = [
'.fluidvids {',
'width: 100%; max-width: 100%; position: relative;',
'}',
'.fluidvids-item {',
'position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;',
'}'
].join('');
var head = document.head || document.getElementsByTagName('head')[0];
function matches (src) {
return new RegExp('^(https?:)?\/\/(?:' + fluidvids.players.join('|') + ').*$', 'i').test(src);
}
function getRatio (height, width) {
return ((parseInt(height, 10) / parseInt(width, 10)) * 100) + '%';
}
function fluid (elem) {
if (!matches(elem.src) || !!elem.getAttribute('data-fluidvids')) return;
var wrap = document.createElement('div');
elem.parentNode.insertBefore(wrap, elem);
elem.className += (elem.className ? ' ' : '') + 'fluidvids-item';
elem.setAttribute('data-fluidvids', 'loaded');
wrap.className += 'fluidvids';
wrap.style.paddingTop = getRatio(elem.height, elem.width);
wrap.appendChild(elem);
}
function addStyles () {
var div = document.createElement('div');
div.innerHTML = '<p>x</p><style>' + css + '</style>';
head.appendChild(div.childNodes[1]);
}
fluidvids.render = function () {
var nodes = document.querySelectorAll(fluidvids.selector.join());
var i = nodes.length;
while (i--) {
fluid(nodes[i]);
}
};
fluidvids.init = function (obj) {
for (var key in obj) {
fluidvids[key] = obj[key];
}
fluidvids.render();
addStyles();
};
return fluidvids;
});
fluidvids.init();