0

I'm trying to load a js and a css file from within a javascript in chrome I can see that both files are loaded, the mofo alert is triggered, and then the test alert, but not the test2 alert.

Why is that, is there an error in the javascript I can't see?

function loadScript(url, callback){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    script.onreadystatechange = callback;
    script.onload = callback;
    document.getElementsByTagName('script')[0].appendChild(script);
}

function loadCss(url, callback){
    var elem = document.createElement('link');
    elem.href = url;
    elem.type = 'text/css';
    elem.rel = 'stylesheet';
    elem.media = 'all';
    document.getElementsByTagName('script')[0].appendChild(elem);
}

var coptaJQStart = function() {
    var jQuery_1_11_3 = $.noConflict(true);
    alert('test');
};

var coptaCssStart = function() {
    alert('test2');
};


loadScript('https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', coptaJQStart);
loadCss('http://www.tffan.com/copta.css', coptaCssStart);
alert('mofo');
3
  • It doesn't seem that you're executing the callback() function in loadCss(). Commented Sep 9, 2016 at 18:59
  • You have .onload set for you script element, but not setting one for your css (link) element Commented Sep 9, 2016 at 18:59
  • 1
    You're also appending the script and link to the first script tag, which won't work. Maybe you meant insertAfter? Commented Sep 9, 2016 at 19:03

1 Answer 1

1

You have to attach call back function in loadCss

 elem.onreadystatechange = callback;
    elem.onload = callback;

function loadScript(url, callback){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    script.onreadystatechange = callback;
    script.onload = callback;
    document.getElementsByTagName('script')[0].appendChild(script);
}

function loadCss(url, callback){
    var elem = document.createElement('link');
    elem.href = url;
    elem.type = 'text/css';
    elem.rel = 'stylesheet';
    elem.onreadystatechange = callback;
    elem.onload = callback;
    elem.media = 'all';
    document.getElementsByTagName('script')[0].appendChild(elem);
}

var coptaJQStart = function() {
    var jQuery_1_11_3 = $.noConflict(true);
    alert('test');
};

var coptaCssStart = function() {
    alert('test2');
};


loadScript('https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', coptaJQStart);
loadCss('http://www.tffan.com/copta.css', coptaCssStart);
alert('mofo');

Sign up to request clarification or add additional context in comments.

5 Comments

What lines? Nobody likes to play find the line of code in the haystack. Explain your answers please.
the op has not called the callback function in 'loadCss' function thats why the callback is not happening..Please run my code you will get what OP expects.
As mentioned by @MikeMcCaughan, this is "appending the script and link to the first script tag, which won't work."
no, the first script tag thing seems to work fine now all I needed was the onload bit for the callback.
Fair enough; your browser may be able to figure it out. But the structure is definitely not correct: <script><script></script><link></script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.