8

I'd like to load a stylesheet when my URL variable contains "?view=full".

Is it possible to do this in HTML (i.e. not PHP)? If so, how?

Ryan
  • 5,523
  • 16
  • 48
  • 84
  • Can you use JavaScript as well, or just HTML? – rid May 26 '11 at 23:34
  • 2
    Are you trying to have different CSS for a normal view and a print view? It may be better to use [CSS media types](http://www.w3.org/TR/CSS2/media.html), which allow you to specify different stylesheets for screen, mobile, and print. – josh3736 May 26 '11 at 23:36

2 Answers2

11

It’s not possible in pure HTML; you'd have to use either PHP or JavaScript. If you want to do it in JavaScript, you could put this in your <head> section:

<script>
if (window.location.search.indexOf('?view=full') === 0)
    document.write('<link rel="stylesheet" href="theStylesheet.css" />');
</script>
Ry-
  • 199,309
  • 51
  • 404
  • 420
4

This will create the link element in your head element if that GET param is present.

if (window.location.search.search(/[?&]view=full(?:$|&)/) !== -1) {
    var link = document.createElement('link');
    link.type = 'text/css'; 
    link.rel = 'stylesheet';
    link.href = 'path/to/it.css';
    document.getElementsByTagName('head')[0].appendChild(link);
}
alex
  • 438,662
  • 188
  • 837
  • 957