Forum Moderators: coopster
I looked into using a session variable, too, but that looks like it won't work, either.
What I'm trying to do is make my adsense "alternate URL" which is javascript use a variable.
You are correct, it's too late to do it with server-side scripts.
You could(BUT DON'T) load both versions into js variables on the server and then use js to serve the proper one. However, you would need to "alter" the Adsense code to do it.
The only other option I see is using sessions, and you'll have to wait for the second page load for that to take affect.
I'm all ears for a solution to this!
The first thing your PHP page does is check for a session variable with the monitor size.
If it doesn't find it (e.g., when the visitor first arrives) PHP outputs a blank page, which runs a Javascript to determine the monitor size, and then sends that data back to the PHP page with the monitor size info in the query string.
If it does find the session info, PHP generates the page with the correct info for the monitor size (using a PHP variable to place the correct monitor info into JavaScript).
But - there is another way. Put your banner in an iframe, and use javascript to populate it with whatever banner will fit best in the space provided.
<iframe name="myframe" width="100%" height="80">
<script>
if (screen.width<600){
myframe.src="smallbanner.php";
}else{
myframe.src="largebanner.php";
}
</script>
I see what you mean though about passing a client (javascript) variable into the ad. Maybe you could write a javascript variable on the main page
<script>
window.top.myvariable='hello world';
</script>
then retrieve it in the ad
<script>
alert(window.top.myvariable);
</script>
not sure if this is obvious, but your adSense alternate URL can be a totally PHP-ized script.
Yes, I realized that after I posted the question (the current include is .inc and the PHP I tried isn't working probably because it's not being parsed - duh).
I'm going to try using a PHP page with a database call in it as the alternate URL.
Thanks!