0

I got some condition, so I can not load some css, js in html. So I decide that pulling dynamically with $.get(). However, I tested js case, but do not have any clues in case of css.

Following is pulling and execute js from remote CDN dynamically.

Pulling and Execute https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js'>

Is it possible to get Other javascript file from remote?

<pre>
    <code class='java'>
        System.out.println("It's done!");
    </code>
</pre>

<script>
    const highlightCDN = 'http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/highlight.min.js';
    $(function(){
        $.get(highlightCDN, function(data){
            console.log('done!');
            eval(data);
        }).done(function(){
            $('pre code').each(function(i, block){
                console.log(i);
                hljs.highlightBlock(block);
            });
        });
    });
</script>

However, I can not use eval on css. What can I do? Is it even possible? Thanks so much!

P.S. If I pulling js like my example, can I use it globally after once I pulled this?

3
  • Is your CSS in a .css file? (I think you can dinamically create a <link rel...> tag Commented Mar 7, 2016 at 11:35
  • see [this question[(stackoverflow.com/questions/524696/…) for more options Commented Mar 7, 2016 at 11:38
  • Thanks, guys. My brain was stuck. Commented Mar 7, 2016 at 12:13

1 Answer 1

1

You can insert a link element via jQuery, which points to your external css file like this:

$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', 'external-css-stylesheet-url-goes-here'));
Sign up to request clarification or add additional context in comments.

1 Comment

Yeap. Thanks. I was idiot -_-; I've been trying to execute css for an hour. Thanks a lot!!

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.