Adsense within script

K

kodknackaren

Hello,

I am trying to write a javascript that will load an Adsense banner.
Everything works with the default syntax but here I want to load the ad
on certain conditions so I want a certain script for it.

In this example I have removed all conditions and am only trying to get
the banner to load but nothing happens.

Can anyone see what I have missed?

<html>
<head>
<script type="text/javascript">
function test()
{
google_ad_client = "pub-6934422475561575";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "336699";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
var element=document.createElement("script");
element.setAttribute("src",
"http://pagead2.googlesyndication.com/pagead/show_ads.js");
document.body.appendChild(element);
}
</script>
</head>
<body onload="test()">
</body>
</html>

Here is the default script that works but I don't know a way to show it
conditionally other than my attempt above:

<script type="text/javascript"><!--
google_ad_client = "pub-6934422475561575";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "336699";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
 
R

Randy Webb

(e-mail address removed) said the following on 4/24/2006 3:49 AM:
Hello,

I am trying to write a javascript that will load an Adsense banner.
Everything works with the default syntax but here I want to load the ad
on certain conditions so I want a certain script for it.

var element=document.createElement("script");
element.setAttribute("src",
"http://pagead2.googlesyndication.com/pagead/show_ads.js");

Don't use setAttribute. Search the archives for "setAttribute buggy IE".

element.src = "http:....."
 
K

kodknackaren

Don't use setAttribute. Search the archives for "setAttribute buggy IE".
element.src = "http:....."

Thanks for your reply. Unfortunately this does not help. I changed the
code to this:

<html>
<head>
<script type="text/javascript">
function test()
{
google_ad_client = "pub-6934422475561575";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "336699";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
var element=document.createElement("script");
element.src =
"http://pagead2.googlesyndication.com/pagead/show_ads.js";
document.body.appendChild(element);
};
</script>
</head>
<body onload="test()"></body>
</html>

....and the result is the same. Nothing happens. In Firefox it says
"Stopped" in the status line and page is just in a loading state. In IE
it just says "Done" with a blank page.
 
R

rob c

In message <[email protected]> -
(e-mail address removed) writes:
:>
:>Hello,
:>
:>I am trying to write a javascript that will load an Adsense banner.
:>Everything works with the default syntax but here I want to load the ad
:>on certain conditions so I want a certain script for it.
:>
:>In this example I have removed all conditions and am only trying to get
:>the banner to load but nothing happens.
:>
:>Can anyone see what I have missed?
:>
:>

Hi

I had the same problem and fixed it by moving the adsense stuff into a
seperate .js file and calling it as a function. The "adsense" function gets
called all the time, but the ads only appear under certain conditions. The
other bonus with this approach is that all my adsense ads are together in a
single file.

Here's what I'm using...

// adsense.js
// Include Google AdSense code instead of adding code to page.
// This module contains routines for all google ads.
// Dec 27/05

// calling code
// <script
language='JavaScript'type='text/javascript'>adsense('abcd');</script>


// if the cookie is set to 'no' then just return without showing an adsense
ad.
function adsense(what) {
if (0 <= parseInt(document.cookie.indexOf('RCPCountMe=no',0))) {
document.write("<font color='Gray'>");
document.write("<hr width=130>");
document.write("AdSense&nbsp;bypassed");
document.write("<hr width=130><\/font>");
return;
}

//define common values
valid_ad = "n"; show_header="n";
google_ad_client = "pub-0880936855579927";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "000000";

// Small square format 125w x 125h
if (what=='b125') {
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as";
google_color_bg = "D3D3D3";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
valid_ad = "y"; show_header="y";
}

// Basic banner 730w x 90h
if (what=='banner') {
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_color_bg = "D3D3D3";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
valid_ad = "y"; show_header="y";
}


if (show_header=="y") {
document.write("<P Class='AdSense' Align=Left>Sponsored Links...</P>");
}

if (valid_ad=="y") {
document.writeln("<scr"+"ipt type='text/javascript'
src='http://pagead2.googlesyndication.com/pagead/show_ads.js'></scr"+"ipt>");
}
}

Hope this helps.

Rob
www.rcp.ca
 
T

Thomas 'PointedEars' Lahn

rob said:
[...] (e-mail address removed) writes:
[...]
:>I am trying to write a javascript that will load an Adsense banner.
:>Everything works with the default syntax but here I want to load the ad
:>on certain conditions so I want a certain script for it.
:>
:>In this example I have removed all conditions and am only trying to get
:>the banner to load but nothing happens.
:>
:>Can anyone see what I have missed?
[...]

Please learn to quote: Use one leading quotation character for one quotation
level only. Everything else confuses properly configured newsreaders. And
trim your quotes to the minimum necessary for retaining context next time.

<URL:http://jibbering.com/faq/faq_notes/pots1.html>
[...]
// <script
language='JavaScript'type='text/javascript'>adsense('abcd');</script>

The `language' attribute for the `script' element is deprecated long since.
And attribute-value pairs should be delimited by whitespace.
// if the cookie is set to 'no' then just return without showing an
adsense
ad.

Code should be posted so that it still works when it was subject to
word-wrap.
function adsense(what) {
if (0 <= parseInt(document.cookie.indexOf('RCPCountMe=no',0))) {

There is exactly no point in parseInt() here (especially not without a
base argument). String.prototype.indexOf() will always return a number.
Besides, `value > -1' would suffice.
document.write("<font color='Gray'>");
document.write("<hr width=130>");
document.write("AdSense&nbsp;bypassed");
document.write("<hr width=130><\/font>");
return;
}

This is generating invalid markup. The `font' element, which is deprecated,
too, is an inline-level element. The `hr' element is a block-level
element. It is common knowledge that inline-level elements must not
contain block-level elements:

,-[HTML 4.01 Transitional
| <URL:www.w3.org/TR/html4/sgml/loosedtd.html#basefont>]
|
| [...]
| <!ELEMENT FONT - - (%inline;)* -- local change to font -->
| [...]
| <!ENTITY % inline
| "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
| [...]
| <!ENTITY % fontstyle
| "TT | I | B | U | S | STRIKE | BIG | SMALL">
| [...]
| <!ENTITY % phrase
| "EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE | ABBR |
| ACRONYM" >
| [...]
| <!ENTITY % special
| "A | IMG | APPLET | OBJECT | FONT | BASEFONT | BR | SCRIPT |
| MAP | Q | SUB | SUP | SPAN | BDO | IFRAME">
| [...]
| <!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
| [...]
| <!ENTITY % block
| "P | %heading; | %list; | %preformatted; | DL | DIV | CENTER |
| NOSCRIPT | NOFRAMES | BLOCKQUOTE | FORM | ISINDEX | HR |
^^
| TABLE | FIELDSET | ADDRESS">
| [...]

Besides, the color of the `hr' element's horizontal rule is not supposed to
be affected by the `color' attribute of the `font' element; it is not text.

//define common values
valid_ad = "n"; show_header="n";
google_ad_client = "pub-0880936855579927";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "000000";

// Small square format 125w x 125h
if (what=='b125') {
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as";
google_color_bg = "D3D3D3";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
valid_ad = "y"; show_header="y";
}

// Basic banner 730w x 90h
if (what=='banner') {
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_color_bg = "D3D3D3";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
valid_ad = "y"; show_header="y";
}

Neither identifier is declared, which is known to be error-prone.
I can only hope that the Google AdSense declares them somewhere.
if (show_header=="y") {
document.write("<P Class='AdSense' Align=Left>Sponsored Links...</P>");
}

As "you" did above, ETAGO delimiters ("</") must be escaped within CDATA
content, such as within a HTML `script' element: "<\/". But not within
other content, such as an included external script resource, as in this
case.

And it would be easier to make show_header a boolean value:

if (show_header)
{
// ...
}
if (valid_ad=="y") {

See above.
document.writeln("<scr"+"ipt type='text/javascript'
^^^^^^^^^^^
Only ETAGOs are required to be escaped in CDATA content, not the
src='http://pagead2.googlesyndication.com/pagead/show_ads.js'></scr"+"ipt>");
^^^^^^^^^^^^
.... and this is utter nonsense for the reasons mentioned above.
}
}

Hope this helps.

Unlikely.


PointedEars
 
R

rob c

In message <[email protected]> - Thomas 'PointedEars' Lahn
:>
:>rob c wrote:
:>
:>> [...] (e-mail address removed) writes:
:>> [...]
:>> :>I am trying to write a javascript that will load an Adsense banner.
:>> :>Everything works with the default syntax but here I want to load the ad
:>> :>on certain conditions so I want a certain script for it.
:>> :>
:>> :>In this example I have removed all conditions and am only trying to get
:>> :>the banner to load but nothing happens.
:>> :>
:>> :>Can anyone see what I have missed?
:>> [...]
:>
:>Please learn to quote: Use one leading quotation character for one quotation
:>level only. Everything else confuses properly configured newsreaders. And
:>trim your quotes to the minimum necessary for retaining context next time.
:>
:><URL:http://jibbering.com/faq/faq_notes/pots1.html>
:><URL:http://www.safalra.com/special/googlegroupsreply/>
:>
:>> [...]
:>> // <script
:>> language='JavaScript'type='text/javascript'>adsense('abcd');</script>
:>
:>The `language' attribute for the `script' element is deprecated long since.
:>And attribute-value pairs should be delimited by whitespace.
:>
:>>
:>> // if the cookie is set to 'no' then just return without showing an
:>> adsense
:>> ad.
:>
:>Code should be posted so that it still works when it was subject to
:>word-wrap.
:>
:>> function adsense(what) {
:>> if (0 <= parseInt(document.cookie.indexOf('RCPCountMe=no',0))) {
:>
:>There is exactly no point in parseInt() here (especially not without a
:>base argument). String.prototype.indexOf() will always return a number.
:>Besides, `value > -1' would suffice.
:>
:>> document.write("<font color='Gray'>");
:>> document.write("<hr width=130>");
:>> document.write("AdSense&nbsp;bypassed");
:>> document.write("<hr width=130><\/font>");
:>> return;
:>> }
:>
:>This is generating invalid markup. The `font' element, which is deprecated,
:>too, is an inline-level element. The `hr' element is a block-level
:>element. It is common knowledge that inline-level elements must not
:>contain block-level elements:
:>
:>,-[HTML 4.01 Transitional
:>| <URL:www.w3.org/TR/html4/sgml/loosedtd.html#basefont>]
:>|
:>| [...]
:>| <!ELEMENT FONT - - (%inline;)* -- local change to font -->
:>| [...]
:>| <!ENTITY % inline
:>| "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
:>| [...]
:>| <!ENTITY % fontstyle
:>| "TT | I | B | U | S | STRIKE | BIG | SMALL">
:>| [...]
:>| <!ENTITY % phrase
:>| "EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE | ABBR |
:>| ACRONYM" >
:>| [...]
:>| <!ENTITY % special
:>| "A | IMG | APPLET | OBJECT | FONT | BASEFONT | BR | SCRIPT |
:>| MAP | Q | SUB | SUP | SPAN | BDO | IFRAME">
:>| [...]
:>| <!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
:>| [...]
:>| <!ENTITY % block
:>| "P | %heading; | %list; | %preformatted; | DL | DIV | CENTER |
:>| NOSCRIPT | NOFRAMES | BLOCKQUOTE | FORM | ISINDEX | HR |
:> ^^
:>| TABLE | FIELDSET | ADDRESS">
:>| [...]
:>
:>Besides, the color of the `hr' element's horizontal rule is not supposed to
:>be affected by the `color' attribute of the `font' element; it is not text.
:>
:><URL:http://validator.w3.org/>
:>
:>> //define common values
:>> valid_ad = "n"; show_header="n";
:>> google_ad_client = "pub-0880936855579927";
:>> google_ad_type = "text";
:>> google_ad_channel ="";
:>> google_color_border = "000000";
:>>
:>> // Small square format 125w x 125h
:>> if (what=='b125') {
:>> google_ad_width = 125;
:>> google_ad_height = 125;
:>> google_ad_format = "125x125_as";
:>> google_color_bg = "D3D3D3";
:>> google_color_link = "0000FF";
:>> google_color_url = "008000";
:>> google_color_text = "000000";
:>> valid_ad = "y"; show_header="y";
:>> }
:>>
:>> // Basic banner 730w x 90h
:>> if (what=='banner') {
:>> google_ad_width = 728;
:>> google_ad_height = 90;
:>> google_ad_format = "728x90_as";
:>> google_color_bg = "D3D3D3";
:>> google_color_link = "0000FF";
:>> google_color_url = "008000";
:>> google_color_text = "000000";
:>> valid_ad = "y"; show_header="y";
:>> }
:>
:>Neither identifier is declared, which is known to be error-prone.
:>I can only hope that the Google AdSense declares them somewhere.
:>
:>> if (show_header=="y") {
:>> document.write("<P Class='AdSense' Align=Left>Sponsored Links...</P>");
:>> }
:>
:>As "you" did above, ETAGO delimiters ("</") must be escaped within CDATA
:>content, such as within a HTML `script' element: "<\/". But not within
:>other content, such as an included external script resource, as in this
:>case.
:>
:>And it would be easier to make show_header a boolean value:
:>
:> if (show_header)
:> {
:> // ...
:> }
:>
:>> if (valid_ad=="y") {
:>
:>See above.
:>
:>> document.writeln("<scr"+"ipt type='text/javascript'
:> ^^^^^^^^^^^
:>Only ETAGOs are required to be escaped in CDATA content, not the
:><script ...> start tag. This concatenation is unnecessary ...
:>
:>>
:>src='http://pagead2.googlesyndication.com/pagead/show_ads.js'></scr"+"ipt>");
:> ^^^^^^^^^^^^
:>.... and this is utter nonsense for the reasons mentioned above.
:>
:>> }
:>> }
:>>
:>> Hope this helps.
:>
:>Unlikely.
:>
:>
:>PointedEars
:>--
:>Indiana Jones: The Name of God. Jehovah.
:>Professor Henry Jones: But in the Latin alphabet,
:> "Jehovah" begins with an "I".
:>Indiana Jones: J-...

Okay, so I forgot to warn the reader that word wrap might screw up the code.
This code does work when used as suggested and it has been in use for over a
year.

At least I offered him a possible solution.

Rob
 
T

Thomas 'PointedEars' Lahn

rob said:
In message said:
[Full quote]

Okay, so I forgot to warn the reader that word wrap might screw up the
code.

If that was all, there was not really a big problem.
This code does work when used as suggested and it has been in use for over
a year.

"Works for me" is not a reasonable or responsible approach.
Search the archives.
At least I offered him a possible solution.

No, you did not. Your code is simply utter nonsense, and further a
sign of your incompetence.

And you have still not learned to quote, despite you have given enough
pointers.


Score adjusted

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top