Javascript after load css

Hi Experts,

I am currently using the code below to load my css files in (as per google pagespeed guidelines). One of my plugins relies on the css being loaded before it fires. Its a background image plugin.

Is there anyway of setting the code up so the plugin only runs once this has completed?

Thanks in advance

var loadCss = function() {
    var links = ["///fonts.googleapis.com/css?family=Open+Sans:400,600,700", "/core.css", "/main.css"],
        headElement = document.getElementsByTagName("head")[0],
        linkElement, i;
    for (i = 0; i < links.length; i++) {
        linkElement = document.createElement("link");
        linkElement.rel = "stylesheet";
        linkElement.href = links[i];
        headElement.appendChild(linkElement);
    }
};
var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) {
    raf(loadCss);
} else {
    window.addEventListener("load", loadCss);
}

Open in new window

maccaj51Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hans LangerCommented:
I would add an Hidden HTML element, like a div: <div id="cssflag" /> and then evaluate with javascript to see if the css has been loaded:

css file:
#cssflag{display:none}

Open in new window

javascript:
setTimeout(function(){
    if(!$('#cssflag').is(':visible') ){
       //loadJS()
    }
},100)

Open in new window

0
Julian HansenCommented:
Why not load the plugin dynamically as well and put the code to do so after your CSS load?
0
maccaj51Author Commented:
Hi Julian,

Could you give me an example?
0
Julian HansenCommented:
This site gives some example code on how to do this. They use a generic function for loading both css and JS

http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maccaj51Author Commented:
Thats great Julian - Thanks ever so much!!

I'm awarding you the points but just wondering if I could ask u if i need:

var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) {
    raf(loadCss);
} else {
    window.addEventListener("load", loadCss);
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.