Skip to content

Instantly share code, notes, and snippets.

@musaulker
Created August 25, 2010 18:45
Show Gist options
  • Save musaulker/550055 to your computer and use it in GitHub Desktop.
Save musaulker/550055 to your computer and use it in GitHub Desktop.
// Google Ads on a webview
// Titanium Appcelerator Mobile on Android
//create a mainwindow
var mainwindow = Ti.UI.createWindow({
backgroundColor:'white',
});
//create a webview which opens the Google Adsense html file
var advertisement = Titanium.UI.createWebView({
url:'advertisement.html',
bottom:0, //can also be top:0
height:50, //defined in the advertisement.html file
zIndex:1,
});
//add the advertisement webview to the mainwindow
mainwindow.add(advertisement);
//create a url string variable which holds the url of the advertisement webview
var adurl;
//start the watchAd function every 0.5 seconds and checks if the url of the advertisement webview has changed
var interval = setInterval(watchAd, 500);
//function watchAd checks if the advertisement url has changed
function watchAd()
{
//if the url of the advertisement webview is not the advertisement.html file, then it stops the interval and starts the Ad() function
if (advertisement.url != 'file:///android_asset/Resources/advertisement.html' && advertisement.url != 'advertisement.html')
{
Ti.API.info('Ad has been clicked / Advertisement webview url has changed to website: '+advertisement.url);
//the variable adurl is set to the newly received url
adurl = advertisement.url;
//the watchAd function is stopped (or in other words, timer stopped)
clearInterval(interval);
//the ad function is started
ad();
}
};
//defines on how to open the ad url in a new window
function ad()
{
//creating a new window
var adwindow = Titanium.UI.createWindow({
});
//creating a new webview for the new window
var advertisement2 = Titanium.UI.createWebView({
//the url of the new webview is set to the received url of the advertisement
url:adurl,
top:0,
//don't set the height, website will auto its height and width
zIndex:1,
});
//add the new webview to the new window
adwindow.add(advertisement2);
//open the new window (modally)
adwindow.open({modal:true});
//what happens when the new window is closed (back button on android phone is pressed)
adwindow.addEventListener('close', function(e)
{
//reset the original webview url to the advertisement.html file
advertisement.url = 'advertisement.html';
//removes the new webview (don't exactly know yet if this is necessary)
adwindow.remove(advertisement2);
//start the watchAd function again
interval = setInterval(watchAd, 500);
});
};
//the advertisement.html file contents
<html>
<body leftmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
<script type="text/javascript"><!--
window.googleAfmcRequest = {
client: 'blablabla',
ad_type: 'text_image',
output: 'html',
channel: '',
format: '320x50_mb',
oe: 'utf8',
color_border: '336699',
color_bg: 'FFFFFF',
color_link: '0000FF',
color_text: '000000',
color_url: '008000',
};
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_afmc_ads.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment