Download CSS based on the URL variable in the HTML page.

I want to load a stylesheet when my URL variable contains "? View = full".

Can this be done in HTML (i.e. not PHP)? If so, how?

+4
source share
2 answers

Its not possible in pure HTML; You will have to use PHP or JavaScript. If you want to do this in JavaScript, you can put this in your <head> section:

 <script> if (window.location.search.indexOf('?view=full') === 0) document.write('<link rel="stylesheet" href="theStylesheet.css" />'); </script> 
+9
source

This will create a link element in your head element if this GET parameter 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); } 
+3
source

All Articles