1

On my website I display some advertisements and it should be loaded like this:

<div id='ad'>
http://ad.affiliate.com/?id=12345
</div>

But when I use that script, the ad can slow down the loading time of the website. How can I load that script after the page has loaded? I tried this but that doesn't work.

<div id='ad'>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#ad").load("http://ad.affiliate.com/?id=12345");
        });
    </script>
</div>

Thanks!

Jordy
  • 4,719
  • 11
  • 47
  • 81

3 Answers3

0

You should try use the defer attribute. Maybe this could help you: Is it possible to load a javascript ad last on the page to prevent slow load times?

Community
  • 1
  • 1
Gero
  • 12,993
  • 25
  • 65
  • 106
0

Instead of using $(document).ready(), you can use $(window).load()

<div id='ad'>
    <script type="text/javascript">
        $(window).load(function(){
            $("#ad").load("http://ad.affiliate.com/?id=12345");
        });
    </script>
</div>

Hope this works.

Muhammad...

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
-1

Or maybe this thing can do the trick:

$(document).ready(function(){
    setTimeout(function() {
        $("#ad").load("http://ad.affiliate.com/?id=12345");
    }, 0);
});
Mateusz Charytoniuk
  • 1,820
  • 20
  • 31