SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to detect screen resolution and display Google ads based on it?

    Hello guys,

    I'm trying to write code that will dispay Google ads based on screen resolution.

    I'm using PHP and Javascript. I wrote this, but it will never work, because of multiply "<script></script>":

    Code:
    <?
    
    $big = '<script type="text/javascript"><!-- google_ad_client = "pub-11121212121"; google_ad_slot = "'.$slotBig.'"; google_ad_width = 468; google_ad_height = 60; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    $small = '<script type="text/javascript"><!-- google_ad_client = "pub-11121212121"; google_ad_slot = "'.$slotHalf.'"; google_ad_width = 234; google_ad_height = 60; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    
    ?>
    
    <script type="text/javascript">
    <!--
    
    if(screen.width <= 800){
    	document.write('<? print $big; print $small; ?>');
    }else if(screen.width <= 1024){
    	document.write('<? print $big; print $big; ?>');
    }else if (screen.width <= 1280){
    	document.write('<? print $big; print $big; print $small; ?>');
    }else{
    	document.write('<? print $big; print $big; print $big ?>');
    }
    //-->
    
    </script>
    I wonder how can I make JS part work? How can I modify Google Ads' code?

    So

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    PHP could write the variable to javascript, which could then use it.

    How do you go with the following?

    Code JavaScript:
    var big = '<? echo addslashes($big) ?>';
    var small = '<? echo addslashes($small) ?>';
     
    if(screen.width <= 800){
    	document.write(big + small);
    . . .

  3. #3
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    PHP could write the variable to javascript, which could then use it.

    How do you go with the following?

    Code JavaScript:
    var big = '<? echo addslashes($big) ?>';
    var small = '<? echo addslashes($small) ?>';
     
    if(screen.width <= 800){
    	document.write(big + small);
    . . .
    Doesn't work.

    It displays this in my output HTML:

    '; var small = ''; if(screen.width <= 800){ document.write(big + small); }else if(screen.width <= 1024){ document.write(big + big); }else if (screen.width <= 1280){ document.write(big + big + small); }else{ document.write(big + big + big); } //-->

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You'll need to split up the closing script tags.

    Code JavaScript:
    <?php
    $big = '<script type="text/javascript"><!-- google_ad_client = "pub-11121212121"; google_ad_slot = "'.$slotBig.'"; google_ad_width = 468; google_ad_height = 60; //-->';
    $bigload = '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">';
    $small = '<script type="text/javascript"><!-- google_ad_client = "pub-11121212121"; google_ad_slot = "'.$slotHalf.'"; google_ad_width = 234; google_ad_height = 60; //-->';
    $smallload = '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">';
    ?>
    <script type="text/javascript">
    var big = '<?php echo addslashes($big) ?>' + '</scr' + 'ipt>';
    var big = big + '<?php echo addslashes($bigload) ?>' + '</scr' + 'ipt>';
    var small = '<?php echo addslashes($small) ?>' + '</scr' + 'ipt>';
    var small = small + '<?php echo addslashes($smallload) ?>' + '</scr' + 'ipt>';
    document.write(big + small);
    </script>

  5. #5
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It displays empty space like 100% width and 120px height.

    However, there is HTML output like:

    <style type="text/css" media="screen">.gmnoscreen{display:none}</style><style type="text/css" media="print">.gmnoprint{display:none}</style>

    <iframe name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=undefined&amp;dt=1197996837120&amp;lmt=1197996822&amp;format=undefinedxundefined&amp;output=html&amp;correlator=1197996837119&amp;url=http%3A%2F%2Fwww.site.com%2Fru%2Fdestinations%2FFrance%2F2988507%2FParis-travel-guide.html&amp;cc=100&amp;ga_vid=1465327391.1185458225&amp;ga_sid=1197996822&amp;ga_hid=1457765322&amp;ga_fc=true&amp;flash=9&amp;u_h=1200&amp;u_w=1600&amp;u_ah=1130&amp;u_aw=1600&amp;u_cd=24&amp;u_tz=120&amp;u_his=1&amp;u_java=true&amp;u_nplug=9&amp;u_nmime=101" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" frameborder="0" height="" scrolling="no" width=""></iframe><iframe name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=undefined&amp;dt=1197996840342&amp;lmt=1197996120&amp;prev_fmts=undefinedxundefined&amp;format=undefinedxundefined&amp;output=html&amp;correlator=1197996840119&amp;url=http%3A%2F%2Fwww.site.com%2Fru%2Fdestinations%2FFrance%2F2988507%2FParis-travel-guide.html&amp;cc=100&amp;ga_vid=1465327322.1185452275&amp;ga_sid=1192296842&amp;ga_hid=1457765322&amp;ga_fc=true&amp;flash=9&amp;u_h=1200&amp;u_w=1600&amp;u_ah=1130&amp;u_aw=1600&amp;u_cd=24&amp;u_tz=120&amp;u_his=1&amp;u_java=true&amp;u_nplug=9&amp;u_nmime=101" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" frameborder="0" height="" scrolling="no" width=""></iframe>

  6. #6
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you not be better off with window dimension, not screen dimensions because you are assuming the user has 'full screened' their browser.

    Si

  7. #7
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by si@ld View Post
    Would you not be better off with window dimension, not screen dimensions because you are assuming the user has 'full screened' their browser.

    Si
    Is it possible? When I googled, I've found a lot of screen.width tutorials and no any windows dimension. Can you tell me what variables should I use instead of screen.width please?

  8. #8
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw57, it seems your code should work, thanks.

    I've just insert raw Google code into my pages and it doesn't work alos (sorry, I should make it before writing code). It always worked on all my websites without any problems, but they doesn't work on this one.

    I wonder why does it happen?


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
  •