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 8215651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:51:24+00:00 2026-06-07T11:51:24+00:00

I need to load a bunch of CSS files through ajax and call an

  • 0

I need to load a bunch of CSS files through ajax and call an animation when the stylesheet has finished loading, otherwise the animation will fail.

What I have done so and used to work pretty well until I came accross doing this cross-domain is this:

$.get(resource.url, {cache:true}, function(css) {
    //Now that the stylesheet is in the browser cache, it will load instantly:  

    $("head").append($("<link>",{
       rel: "stylesheet",
       type: "text/css",
       href: resource.url
    }));

}).then(function(){
   //Animation here that depends on the loaded css
});

This works fine as long as resource.url is on the same domain. Once I try to load the css from another domain $.get will fail like this:

XMLHttpRequest cannot load https://example.com/style.css. Origin https://example.com is not allowed by Access-Control-Allow-Origin.

So I have tried to add CORS into the header through .htaccess:

<IfModule mod_headers.c>
    #cross domain access is okay for resources (#107)
    <FilesMatch "\.(css|js)$"> 
        Header add Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

This adds the CORS header to all CSS and JS resources.

For some reason CORS doesn’t seem to have an effect on either chrome or firefox (newest versions).

I also came to notice that same domain policy is not enforced when doing $.getScript for js files, but it is for $.get:

$.get("https://example.com/script.js", {cache: false}, $.noop, "script"); 

//works regardless of CORS

but:

$.get("https://example.com/script.js", {cache: false}, $.noop); 

//does not work (cross domain policy violation)

So since CORS is not widely supported and doesn’t even seem solve the problem for a modern browser, I need something that behaves like $.getScript, but for CSS stylesheets.

It needs to be asynchronous and have a callback mechanism.

Any ideas? Thanks in advance…

  • 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-06-07T11:51:25+00:00Added an answer on June 7, 2026 at 11:51 am

    $.get uses Ajax, which must obey the Same-Origin Policy.

    $.getScript normally uses Ajax, but it also has a fallback option for cross-domain use only. Scripts imported via <script> tag are not subject to Same-Origin rules, so jQuery just adds a <script> tag to the page and sets its src attribute to the requested script URL.

    The same exemption is made for CSS resources loaded in through <link> tags. In a perfect world, you should be able to 1) make a new <link> element, 2) set its href attribute to the right URL and 3) listen for a load event on that element. I’ve added a callback to this solution from the jQuery forums:

    // note: non-compatible example code, see below for better code
    jQuery.getCSS = function( url, media, callback ) {
        jQuery( document.createElement('link') ).attr({
            href: url,
            media: media || 'screen',
            type: 'text/css',
            rel: 'stylesheet'
        }).appendTo('head')
        .on("load", callback);
    };
    

    One small problem here: it doesn’t work cross-browser. <link> tags do not fire load events in all browsers — see the jQuery bug report for the $.getCSS function that could have been.

    So, the general-case workaround is a bit insane: add the CSS URL as the source of a new <img> tag and listen for its onerror handler:

    // correct code!
    jQuery.getCSS = function( url, media, callback ) {
        jQuery( document.createElement('link') ).attr({
            href: url,
            media: media || 'screen',
            type: 'text/css',
            rel: 'stylesheet'
        }).appendTo('head');
    
        jQuery( document.createElement('img') ).attr('src', url)
        .on("error", callback);
    };
    

    You could also add a bit of optimization and listen for load event on the <link> (for browsers that support it) and an error event on the <img> (for those that don’t), and put some logic in place to ensure the callback is only ever called once, in case both events occur.

    As for making $.getCSS return a deferred object, that’s a bit out of my area of expertise, but there’s no theoretical reason why it couldn’t be done.

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

Sidebar

Related Questions

Part of our web app has a little Ajax method that will load a
I need to load data into my treestore. My ajax request give me XML
I load a bunch of HTML from the server containing img-tags. I need to
I have a class which has a bunch of Constant Strings. I need to
I have a popup div that I need to load a bunch of different
I need to deserialize a bunch of XML files in a .NET 4.0 project
I'm in need to load an bunch of bitmaps in my application. The problem
I have an ASP page, which on load fires a bunch of AJAX calls.
I need to load a bunch of things in the beginning of my application,
So I'm making a Load.c file, that basically will load a bunch of students

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.