Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 561867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:29:42+00:00 2026-05-13T12:29:42+00:00

$.getScript(‘ajax/test.js’, function() { alert(‘Load was performed.’); }); .. like the above code which loads

  • 0
$.getScript('ajax/test.js', function() {
  alert('Load was performed.');
});

.. like the above code which loads an external JS on request, is there something similar available to load an external CSS stylesheet when required?

Like for example when I use lightboxes (inline popups) on my site, I want to avoid loading lightbox JS and CSS files onload, unless requested by the user.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:29:43+00:00Added an answer on May 13, 2026 at 12:29 pm

    Yup: if you create a <link> tag linking to a stylesheet and add it to the <head> tag, the browser will load that stylesheet.

    E.g.

    $('head').append('<link rel="stylesheet" type="text/css" href="lightbox_stylesheet.css">');
    

    However, as per @peteorpeter’s comments, this doesn’t work in IE 8 or under — there, you need to either:

    1. append the <link> before setting its href; or
    2. use IE’s document.createStyleSheet() method

    Also, checking whether a link has already been added doesn’t work reliably across all browsers.

    I would also question part of your premise:

    I want to avoid loading lightbox JS and CSS files onload, unless requested by the user.

    Why? To reduce page weight? I can understand the desire, but you should measure the size of your CSS and JS files (after minification and gzipping) with the lightbox code in there, and without, to see whether the reduction is worth:

    1. the added complexity of loading on-demand; and
    2. the slightly reduced responsiveness of the lightbox (because when loading on-demand, the lightbox will have to wait for its own CSS and JS to load before it can do its thing)

    After minification and gzipping, there may well not be that much difference.

    And bear in mind that you can instruct the browser to cache your CSS and JS for a long time, meaning it only gets downloaded when a user visits your site with an empty cache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$.getScript(somescript.js, function() { alert('Load Complete'); }); Once loaded, is it cached or is it
I use getScript to load dynamically my plugin: $.getScript('js/code.photoswipe.jquery-3.0.4.min.js', function () { //do magic
// Lightbox $('a.lightbox').click(function () { $.getScript(js/lightbox.js, function () { alert('Load Complete'); $(a.lightbox).lightbox({ 'type': 'iframe',
I have a function which loads up relevant js functions on click using ajax's
$.getScript of Jquery's ajax methods can be used to load and execute javascript from
Is it better (in any way) to use jQuery's getScript to load the external
I have included external js file using getScript. However, I would like to include
I have a callback function that I'd like to execute after 2 separate $.getScript
core.js: var core = { all:{}, load: function(jsUrl) { $.ajaxStup({async, false}); $.getScript(jsUrl); }, init:
this is my demo code : <body onload=initialize()> <script> function initialize(){ var d='adddd' $.getScript('other.js',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.