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&&currenttime < 5) {        document.write("<link rel='stylesheet' href='night.css' type='text/css'>");       }       if (5 <= currenttime&&currenttime < 11) {        document.write("<link rel='stylesheet' href='morning.css' type='text/css'>");       }       if (11 <= currenttime&&currenttime < 16) {        document.write("<link rel='stylesheet' href='day.css' type='text/css'>");       }       if (16 <= currenttime&&currenttime < 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

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -