- This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
Viewing 8 posts - 1 through 8 (of 8 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
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
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
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?
Seconded. Use media queries instead of JavaScript.
Here’s an introduction to media queries that Chris wrote: https://css-tricks.com/css-media-queries/
@rosspenman and @HugoGiraudel, thanks a lot guys.
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:
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”.