0

is it possible to introduce id specific styles or load different css files depending on the "url" you are at?

something like this:

<script>
 if(location.href == 'http://jpftest2.tumblr.com/about'){ 
document.write('<style type='text/css'>
.social {
  display:none;
}

</style>'); 
} 

</script> 

so when a user goes away from the root url to the "/about" page. i want to introduce the style:

   .social {
  display:none;
}

would be better if it was also possible to load a different CSS file altogether depending on the URL?

the different pages on tumblr share the same head so i can't introduce a different css on different pages

2 Answers 2

2

Assuming your site is using something like AJAX (correct?), (otherwise you would put the style-sheets in the HTML <head> of the file), you could something like:

window.onpopstate = function() {
    if(window.location.path == '/about'){ 
        var css = document.createElement('style');
        css.type = 'text/css';
        css.rel = 'stylesheet';
        css.innerHTML = '.social{display:none;}';
        document.getElementsByTagName('head')[0].appendChild(css);
    }
} 

However I wouldn't recommend it.

Sign up to request clarification or add additional context in comments.

Comments

1

You may use the following approach

<script type="text/javascript">
    $(document).ready(function(){

    if(your_url_condition){
            //for IE compatibility
            if (document.createStyleSheet){
                document.createStyleSheet('style.css');
            }
            else {
                $("head").append($("<link rel='stylesheet' href='style.css' type='text/css' media='screen' />"));
            }
        }
    });
</script>

This will work and may help you.

Regards!

3 Comments

Sure, the post is tagget with jQuery.
@felipeclopes hey felipe, i was wondering what do i replace "(your_url_condition) with? something like "location.href == 'jpftest2.tumblr.com/about'" ?
Yes, you should replace with a browser url comparisson. Or maybe a switch case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.