Forum Moderators: open

Message Too Old, No Replies

Top page Banner script

javascript code

         

kaze

1:11 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Hello, I need a script that will be added after </body> and will be displayed at the top of the page.
I know is posible, I have this code :

<script type="text/javascript">
var object = document.createElement('div');
object.innerHTML = 'The banner script is HERE';
var body = document.getElementsByTagName('body')[0];
body.insertBefore(object, body.firstChild);
</script>

The problem with this cod is that is shows in forum phpbb3 in the login page and after is loged in, twice in the header.
Demo problem: <snipped url>

Just so you know, I need to add html script banner and the JS will be placed right before </html> and AFTER </body>.

Can someone help me here ?
Kind Regards.

[edited by: whoisgregg at 1:23 pm (utc) on Aug. 18, 2009]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] :) [/edit]

whoisgregg

1:47 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's showing up twice in the page, I'd check your source code to see if that script is being added twice as well. There's nothing about that code that would result in the object being created twice.

Since you're using phpBB, perhaps there is some problem with how that application is adding the code? Perhaps there's a different template that it should be added to?

kaze

2:00 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



What I think is, the JS is checking the tag <body> and if the JS find the tag multiple time, is adding the banner script.

I can't check the html/template files because this files belong to my users on my free hosting.

The ad server is working corectly, I just need to know how should I fix this.

Somebody told me to try this, but the cod is only for text ads.

<script type="text/javascript">
// <![CDATA[
window.onload = function() {
if ( document.createElement ) {
var body = document.getElementsByTagName("body")[ 0 ];
var fChild = body.getElementsByTagName("*")[ 0 ];
var obj = document.createElement("div");
obj.appendChild( document.createTextNode("The banner script is here."));
body.insertBefore( obj, fChild );
return;
} alert("unsupported features, please upgrade your browser");
return false;
}
// ]]>
</script>

Any ideas ?

whoisgregg

2:18 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If there is more than one <body> tag, than that's a problem. Plus, even your original script is referencing just the first <body> tag that exists (even though there should only ever be one).

That said, if that second code snippet works for you otherwise, it's easy to put html in the created <div>, just change this line:

obj.appendChild( document.createTextNode("The banner script is here.")); 

to:
obj.innerHTML = 'html can go here';