Forums

Get help. Give help. A Web Design and Development Community.

How to add an external CSS file to a page with Javascript

  • # September 18, 2012 at 4:43 pm

    I’m wondering how it would be done to have an input field where you can paste a link to an external stylesheet from anywhere, and click a submit button and have that stylesheet be applied to the page it’s on, very similar to how you can do on codepen.io where you can add in your own external stylesheet. Any help would be appreciated. Thanks!

    # September 18, 2012 at 5:04 pm

    This reply has been reported for inappropriate content.

    you could use the “append()” function in jquery to add it to the element…

    eg:


    $('button#addCss').on('click', function() {
    var uri = $('input#cssUri').val();
    $('head').append(uri);
    });
    # September 18, 2012 at 5:25 pm

    Cool, I’ll have to give that a try. Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We have a pretty good* newsletter.