0

I want to make sure that an affiliate banner ad only loads after all the images are loaded. The banner is just a script tag:

<SCRIPT type="text/javascript" src="cjbannerlink"> </SCRIPT>

I've read a few threads and here's one of the codes that I've tested fiddle:

$(window).bind("load", function() {

    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "cjbannerlink";
    // Use any selector
    $(".1").append(s);
});

Nothing seems to happen. The banner doesn't appear in the div. Would anyone please tell me if it's possible to fire the banner script last?

RedGiant
  • 4,444
  • 11
  • 59
  • 146
  • 1
    Note that `.1` is an invalid selector (CSS ID selectors can't start with a digit). If you're using a selector like that in your real code, that's the problem. – T.J. Crowder Feb 24 '14 at 08:26

1 Answers1

0

Try $(document).ready(function() { // code here }); instead:

 $(document).ready(function() {
    console.log('LOADED');
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = "http://www.qksz.net/1e-itvi";
    // Use any selector
    $(".1").append(s);
});

Click me.

Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30
  • 1
    If `load` doesn't work, `ready` won't work. `load` fires much later than `ready`. – T.J. Crowder Feb 24 '14 at 08:27
  • @NicolaeOlariu I have made sure my adblock is being disable. But the banner doesn't seem to appear in the fiddle. – RedGiant Feb 24 '14 at 08:37
  • As @T.J.Crowder mentioned in the first comment, a CSS selector can't start with a digit. Read more [here](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html) and [here](http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-selectors). So rename the CSS class and both the selector (to not start with a digit) at line `$(".1".append(s))` and try again. Are there any console errors? – Nicolae Olariu Feb 24 '14 at 08:42
  • @NicolaeOlariu I renamed the class and selector to `abc` and saw the message "load" in the console. **[jsfiddle](http://jsfiddle.net/stonecold111/RXrvZ/1000/)** Unfortunately no banner appears in the divs. I also put the original code in the html to compare. – RedGiant Feb 24 '14 at 08:59
  • Please make sure your addblock is paused or disabled and try again. On my side it works fine - [screenshot](http://postimg.org/image/xz50qd9rd/) – Nicolae Olariu Feb 24 '14 at 09:10