PDA

View Full Version : Simple javascript code doesn't work


FatalBreeze
12-27-2008, 01:21 PM
Hello.

I'm trying to use this simple code to display ads:

<script type="text/javascript">
init_banners();
function init_banners()
{
document.getElementById('navabr_720x90').innerHTML = "<script language='javascript' type='text/javascript' src='http://blablabla.com/st?ad_type=ad&ad_size=728x90'><\/script>";
}
</script>

(I put this in footer, and the div is in navbar)

But the ads doesn't show, if i don't use the function, just paste the script so the ads do show so i dont think the problem is with the ads. Even if i replace the ads link with a simple plain text it DOES work.

So how can i fix this?

Thanks in advance.

Marco van Herwaarden
12-27-2008, 02:02 PM
Is that element id correct?

FatalBreeze
12-27-2008, 02:14 PM
Yes, 100% sure.
I copied and paste it, and i also said that if i change the ".innerHTML = "<script..." to just plain text, it does work.

Deceptor
12-27-2008, 04:06 PM
You can't shove a <script> tag inside a innerHTML property and expect it to load. You could try using createElement() methods and then appendChild(), but I'm not sure the script would load that way either.

Personally with the script you have I'd just do this:
<div id="navbar_720x90" style="display: none;"><script language='javascript' type='text/javascript' src='http://blablabla.com/st?ad_type=ad&ad_size=728x90'></script></div>

<script type="text/javascript">
<!--
function init_banners()
{
document.getElementById('navbar_720x90').style.dis play = '';
}

init_banners();
-->
</script>

Marco van Herwaarden
12-27-2008, 04:19 PM
Still using both "navbar" and "navabr" as id.

Deceptor
12-27-2008, 04:47 PM
Never saw that typo, thanks Marco :)

FatalBreeze
12-27-2008, 05:19 PM
I can't use this script because the purpose of the function i built was to load the ads, as the latest thing on the page. because they are located on an external sever (nothing i can do about that), and because it takes them too much time to load they make the page stuck, so until they are finally load, you can't see the rest of the page. after they load completely the rest of the page shows up. and this process takes about 15 seconds more than i want to. Do you have a better way to fix it?

Deceptor
12-27-2008, 05:58 PM
Well after testing, it seems createElement() does work for loading JS files after the page has loaded, which is nice :)

Try this:
<script type="text/javascript">
<!--

function load_ads(url, appendobj)
{
ads = document.createElement('script');
ads.src = url;
ads.type = 'text/javascript';

document.getElementById(appendobj).appendChild(ads );
}

load_ads('YOUR_SCRIPT_URL', 'YOUR_APPEND_OBJECT');

-->
</script>

Change:
load_ads('YOUR_SCRIPT_URL', 'YOUR_APPEND_OBJECT');

For the url, use the URL to the js file, as for "YOUR_APPEND_OBJECT", it would be: navbar_720x90 :)

FatalBreeze
12-27-2008, 06:46 PM
Thank you very much Deceptor...
but it doesn't work :(

Deceptor
12-27-2008, 07:09 PM
From what I can tell, the script you pass into it doesn't work for some reason. But I know if i pass a test script it executes fine, there's really nothing more I can think of unfortunately.

FatalBreeze
12-27-2008, 07:59 PM
Isn't there something that can be done by AJAX or any other manipulation to make the ads show up only after the page is loaded?