Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How to load CSS using javescript

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #42165
    kiddo
    Participant

    Hi guys,

    i want to ask, is it possible to load css using javascript ???

    because i have 2 css files, 1 for the desktop and the other one for the mobile browser/touch device

    #121827
    rosspenman
    Participant

    It is indeed possible.

    var s = document.createElement(‘link’);
    s.setAttribute(‘href’, ‘style.css’);
    document.head.appendChild(s);

    But sometimes using JavaScript isn’t the best idea. Have you thought about using media queries instead?

    #121828
    Kitty Giraudel
    Participant

    Seconded. Use media queries instead of JavaScript.

    #121830
    rosspenman
    Participant

    Here’s an introduction to media queries that Chris wrote: https://css-tricks.com/css-media-queries/

    #121832
    kiddo
    Participant

    @rosspenman and @HugoGiraudel, thanks a lot guys.

    #121856
    Paulie_D
    Member

    The issue here is that there are two CSS files targeted specifically for different environments, desktop & mobile.

    Media queries could reduce this to a single CSS file but if the separate files are required then there is no reason that the ‘media query’ couldn’t be in the head of the page to load the css file appropriate to the screen/device width.

    Something like:

    #121887
    Paulie_D
    Member

    media = all will media types of which there are many.

    If you want to limit it to screen devices (say you don;t want it for printing purposes) then you could change it to media=”screen”.

    #121965
    kiddo
    Participant

    @Eric, why did you say “I disagree using css is many ways better”, can you explain it a little bit ?

    i’m still new so there’s many thing i must learn


    @Paulie_D
    thanks for the answer

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.