css - Change stylesheet based on Javascript if else statement - Stack Overflow

I'm making a page with lots of iframes on it, the websites in the iframes have ads in them and to

I'm making a page with lots of iframes on it, the websites in the iframes have ads in them and to make the page more viewable to people with adblock, I need to display different stylesheets.
Basically what I want to do is.. if adblock (display this stylesheet) else (display this stylesheet).

I have the code working to detect if adblock is present, but when I try to do the if else statement..the whole page is blank. Here's the code:

<script>
(function(){
    if($("#fakead").css('display')=="none")
    {
        document.write("<link href='css/adblock.css' rel='stylesheet' type='text/css'>")
    }
    else
    { 
        document.write("<link href='css/noadblock.css' rel='stylesheet' type='text/css'>")
    }
});
</script>

I'm extremely new to Javascript so my apologies if the code is terrible.

Thanks for any help

I'm making a page with lots of iframes on it, the websites in the iframes have ads in them and to make the page more viewable to people with adblock, I need to display different stylesheets.
Basically what I want to do is.. if adblock (display this stylesheet) else (display this stylesheet).

I have the code working to detect if adblock is present, but when I try to do the if else statement..the whole page is blank. Here's the code:

<script>
(function(){
    if($("#fakead").css('display')=="none")
    {
        document.write("<link href='css/adblock.css' rel='stylesheet' type='text/css'>")
    }
    else
    { 
        document.write("<link href='css/noadblock.css' rel='stylesheet' type='text/css'>")
    }
});
</script>

I'm extremely new to Javascript so my apologies if the code is terrible.

Thanks for any help

Share Improve this question asked Nov 1, 2013 at 13:50 KerblooyKerblooy 2191 gold badge4 silver badges14 bronze badges 1
  • A simple reminder: when using document.write(), you are basically writing to the webpage. So there's a lesson for the day :) – user2277872 Commented Nov 1, 2013 at 13:55
Add a ment  | 

2 Answers 2

Reset to default 4

You should not use document.write method. As I see you use jQuery, so in jQuery you can do like this:

$( function() {
    if($("#fakead").css('display')=="none") {
        $("<link href='css/adblock.css' rel='stylesheet' type='text/css'/>").appendTo( 'head' );
    } else { 
        $("<link href='css/noadblock.css' rel='stylesheet' type='text/css'/>").appendTo( 'head' )
    }
});

Please note: link element should be placed in <head> section. Read more in w3c.

Pure JS option:

var el = document.createElement("link");
el.type = "text/css";
el.rel = "stylesheet";
el.href = "style.css";
document.getElementsByTagName("head")[0].appendChild(el);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328884a4622784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信