OarSmaN
OarSmaN

Reputation: 53

How can I load advertisements after content?

sometimes my ads causing my site loding very slowly and content is not showing up. i tried this for loading the top ad after the content but it did not work;

<code><div id=”top_ad_loader” style=”display:none;”>adcode</div><script type=”text/javascript”>document.getElementById(“top_ad”).innerHTML =document.getElementById(“top_ad_loader”).innerHTML</script>

any ideas? thanks a lot

Upvotes: 2

Views: 149

Answers (3)

Peter
Peter

Reputation: 5156

Simply load the ads inside iframes. They won't slow down the page load.

Upvotes: 2

Christophe
Christophe

Reputation: 28114

The technique you tried - innerHTML on a script tag - just won't work.

Instead, you would need to add the script dynamically after the page loads:

var newScript=document.createElement("script");
newScript.src="script.js";
document.head.appendChild(newScript);

Unfortunately often this won't work directly on the main page because the ad needs to run at a specific location. In this case, a workaround is to use an iframe. This is what I do in the right hand side of this page.

Upvotes: 0

Ibu
Ibu

Reputation: 43810

Loading 3rd party ads after the page is loaded is not an easy task. Because most ads use document.write() to add their content. When document.write is used after the site is loaded causes the whole page to go blank.

One thing to do is to overwrite document.write on top of the page and manipulate the content yourself.

Example:

// top of the page
var adContent = [];
    document.write = function (a){
    adContent.push(a);
}

Now all your ads data is in the adContent array, you can check the content and append it to the write part.

Upvotes: 0

Related Questions