Hello,
I’m dynamically loading some scripts, and they are not running : ) I think my lack of knowledge of how browsers load/run stuff is my problem. If anyone can enlighten me, link to something, would be great.
I thought I’d give Nicholas Zakas’ isMedia() a try, which allows you to run scripts based on whether the browser is applying a media query or not. I have a test page running a script called “jquerylightbox.js” which is a single file containing a minified jquery library plus the Lightbox 2 script (why make the server do two calls? And we decided not to make one call to some CDN and another call to the lightbox, since anyone not cached are doing just that… and it’s already now an older jquery version so people are more likely to have newer versions cached anyway).
The original setup (calling this jquerylightbox.js in the head) works in the first browsers I’m testing (Firefox 3 and newest Chrome for Linux, whichever version that is).
The Lightbox part dies horribly on mobile, which is fine: I’d rather mobiles get default behaviour (user clicks on image and goes to just that image, need to hit back button or swipe “back” to get back to the main page). In fact, I’d rather mobiles or anything with a small screen doesn’t even bother making the request for the Javascript at all. Why? Waste of bandwidth, which for some people is hideously expensive. So, save them time and money.
So I hacked together Zakas’ isMedia in a window.onload and when Viewing Generated Source and checking what I load, I’m getting the markup I need and the scripts are getting loaded… but they are not getting run.
(code subject to change as I’m currently playing with it)
link to page
/* Copyright 2011 Nicholas C. Zakas. All rights reserved.
* Licensed under BSD License.
* https://gist.github.com/08602e7d2ee448be834c */
var Brandweer = {
init: function() {
var isMedia = (function() {
var div;
return function(mquery) {
if (!div) {
div = document.createElement('div');
div.id = 'poez';
div.style.cssText = 'position:absolute;top:-1000px';
document.body.insertBefore(div, document.body.firstChild);
}
div.innerHTML = '_<style media="' + mquery + '"> #poez {width: 1px;}</style>';
div.removeChild(div.firstChild);
return div.offsetWidth == 1;
};
})();
if (isMedia('screen and (min-width:751px)')) {
var script1 = document.createElement('script');
script1.src = 'jquery.js';
var script2 = document.createElement('script');
script2.src = 'lightbox.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(script1);
head.appendChild(script2);
}
}
};
window.onload = Brandweer.init;
This particular version I’m loading the jquery and lightbox stuff separately for testing (I tried a version where the jquery library is handwritten in the source, and the isMedia script only calls the lightbox… this worked in Chrome but not in Firefox, no idea why).
I’m assuming my problem is, the jQuery is waiting for the page to load (when it already has)? As for why it works in Chrome when I leave everyone to load the jQuery and let the lightbox get added dynamically… maybe that has to do with FF being different in how it reports the load-event setting??
The lightbox stuff is checked by scrolling to the bottom of the page and clicking one of the smaller thumb images.