SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change javascript entry depending on CSS Stylesheet

    Over here I have a page on which the user can change the stylesheet to be a dark style or light style.

    On that page, as you can see, are two Google AdSense units but, since Google does not allow customisation of the ad units with CSS, I have to create two seperate ad units. One for the dark layout and one the light layout. Therefore, I need to create a javascript function that essentially says if(style=main-default.css) then show this ad else show the dark ad.

    Is there a cleancut JS function that does that (check which style sheet is in use)?

    Edit: My two stylesheets are being called as follows:

    <style type="text/css" media="all">@import url(css/main-changes.css);</style>
    <style type="text/css" media="screen, projection">@import url(css/main-default.css);</style>
    Last edited by Bravogolf; Apr 25, 2007 at 06:30. Reason: Better phrasing ;)

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,269
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    style - javascript

    Rather than have have javascript determine what stylesheet is being used, it may be easier to set a variable when you set the style sheet and test for that. eg.
    function setStyle(){
    var css_choice = 'dark';
    .....
    function showAd(){
    if ( css_choice == 'dark'){
    .....

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you want to check what media is currently used so you can execute appropriate javascript. Before that, have you verify that javascript works on all media, for example when users intend to display print preview, print media should be used. Will javascript execute?

  4. #4
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Rather than have have javascript determine what stylesheet is being used, it may be easier to set a variable when you set the style sheet and test for that. eg.
    function setStyle(){
    var css_choice = 'dark';
    .....
    function showAd(){
    if ( css_choice == 'dark'){
    .....
    The javascript used to switch styles is beyond my understanding. I had been hoping for a simple JS function to say if I'm using this style sheet then go ahead and show this ad

    SimplyFu - If makes more sense to check against media then yes. As above, just a clean cut function to check stylesheets and display ads accordingly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •