I'm trying to experiment with an Adsense unit in the footer section of my SmartGWT application. I've tried 2 different approaches of adding the Adsense unit so far, but both didn't work.
Approach 1: Directly embed the Adsense javascript code in an HTMLPane. Here's the code:
After the application loads, it immediately disappears and the screen goes blank. In the status bar, I can see a message "Read googleads.g.doubleclick.net" and the application doesn't comes up again. I've no idea what's going on in this case.
Approach 2: Use a div element and embed the adsense unit with window.onload. Here's the code:
In the main HTML file, I use a javascript to embed the Adsense code:
When the page loads, the ad doesn't appear. In fact, with this approach, even if I set the innerHTML to simple text like "abc", it doesn't work. I'm sure I'm doing something fundamentally wrong in this approach. I've tried to use this approach in the past for embedding an iframe (like a Facebook Like button), but could not get it to work.
Even if I don't get Adsense to work with SmartGWT, I would love to get the second approach working for many reasons, such as separating the HTML from the Java code.
I'll appreciate any help. Thanks.
Approach 1: Directly embed the Adsense javascript code in an HTMLPane. Here's the code:
Code:
HTMLPane ht = new HTMLPane(); String content = "<script type=\"text/javascript\"><!--\n google_ad_client = \"pub-XXXXXXXXXXXXXXXX\";\n /* 728x15, created 6/2/09 */\n google_ad_slot = \"5369522702\";\n google_ad_width = 728;\n google_ad_height = 15;\n //-->\n </script>\n <script type=\"text/javascript\"\n src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n </script>\n"; ht.setContentsType(ContentsType.PAGE); ht.setContents(content); ht.setWidth(800); ht.setHeight(50); ht.setOverflow(Overflow.HIDDEN);
Approach 2: Use a div element and embed the adsense unit with window.onload. Here's the code:
Code:
HTMLPane ht = new HTMLPane(); String content = "<div id=\"adsense\"></div>"; ht.setContentsType(ContentsType.PAGE); ht.setContents(content);
Code:
function embedAdsense() { if (document.getElementById("adsense")) { var content=""; content += "<script type=\"text/javascript\"><!--\ngoogle_ad_client = \"pub-XXXXXXXXXXXXXXXX\";\n/* 728x15, created 6/2/09 */\ngoogle_ad_slot = \"5369522702\";\ngoogle_ad_width = 728;\ngoogle_ad_height = 15;\n//-->\n</script>\n<script type=\"text/javascript\"\nsrc=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n</script>"; document.getElementById("adsense").innerHTML = content; } } window.onload = embedAdsense;
Even if I don't get Adsense to work with SmartGWT, I would love to get the second approach working for many reasons, such as separating the HTML from the Java code.
I'll appreciate any help. Thanks.