Yup: if you create a <link> that references a stylesheet and add it to the <head> , the browser will load this stylesheet.
eg.
$('head').append('<link rel="stylesheet" type="text/css" href="lightbox_stylesheet.css">');
However , according to the comments of @peteorpeters , this does not work in IE 8 or lower - you need to either:
- add
<link> before installing href ; or - use IE
document.createStyleSheet() method
In addition, checking whether a link has been added does not work reliably in all browsers.
I would also question some of your assumption:
I want to not download downloadable JS and CSS files for upload to the lightbox, unless the user has requested.
Why? Reduce page weight? I can understand this desire, but you have to measure the size of your CSS and JS files (after mini-coding and gzipping) using the lightbox code there and without to see if the reduction is worth it:
- Added on-demand download complexity and
- slightly reduced lightbox responsiveness (because when loading on demand, the lightbox will have to wait for its own CSS and JS to load before it can do this).
After minimization and gzipping, maybe not so much.
And remember that you can instruct the browser to cache your CSS and JS for a long time, that is, it only loads when the user visits your site with an empty cache.
Paul D. Waite Jan 24 2018-10-10T00: 00Z
source share