javascript - getStylesheet depending if class exists -
i need add css stylesheet if css class exists in page.
i.e. editing following js:
<script> <!-- function getstylesheet() { var currenttime = new date().gethours(); if (0 <= currenttime&¤ttime < 5) { document.write("<link rel='stylesheet' href='night.css' type='text/css'>"); } if (5 <= currenttime&¤ttime < 11) { document.write("<link rel='stylesheet' href='morning.css' type='text/css'>"); } if (11 <= currenttime&¤ttime < 16) { document.write("<link rel='stylesheet' href='day.css' type='text/css'>"); } if (16 <= currenttime&¤ttime < 22) { document.write("<link rel='stylesheet' href='evening.css' type='text/css'>"); } } getstylesheet(); --> </script>
rather above times, need add css file if certian class present in page - i.e.
if class="company" - use company.css
if class="contact" - use contact.css
if class="enterprise" - use enterprise.css
if class="media" - use media.css
how this?
if can use jquery, can determine if class present on page using following:
if ($('.class').length){ // code }
substituting in .class
whatever class you're attempting see if present.
so:
if ($('.company').length){ document.write("<link rel='stylesheet' href='company.css' type='text/css'>"); } if ($('.contact').length){ document.write("<link rel='stylesheet' href='contact.css' type='text/css'>"); } etc...
Comments
Post a Comment