0

I'm trying to get a jQuery script to load in an external JavaScript file based on if a cookie which I created exists, and it's doing weird things. It is either blanking the entire page and ONLY including my script, or just flat out not working at all.

This is what I have - any help would be tremendously appreciated:

I tried it with only $.getScript instead, and what it did, was loaded my file, but replaced the entire HTML of my page with it at runtime instead of appending the script to the html file.

I've been googling this to death, LOL and just can't get it figured out.

<script>
if($.cookie("so-affiliate") ) {
    $().ready(function() {
    // load the CJ file
    $('#cj-placeholder').append("TEST<script type='text/javascript'>" + "$.getScript('/v/js/cjscript.js');" + "</" + "script>");
});

}else{
  $('#cj-placeholder').append(".");


}

</script>
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
CVP
  • 11
  • 1
  • 3
  • 1
    How are you loading it? Any reason you're not saying the script as a `.js` file and using jQuery's `$.getScript`? – Benjamin Gruenbaum Jun 20 '13 at 22:43
  • You cannot append script tags as text with jQuery since it uses the `.innerHTML` which executes any scripts within script tags. – mekwall Jun 20 '13 at 22:49
  • Thanks for the reply. I tried it with $.getScript instead, and what it did, was loaded my file, but replaced the entire html of my page with it at runtime, instead of appending the script to the html file, it replaced it... – CVP Jun 21 '13 at 13:17

1 Answers1

0

load it with getScript like Benjamin already stated:

if($.cookie("so-affiliate") ) {
    $().ready(function() {
        // load the CJ file
        $.getScript('/v/js/cjscript.js', function () {
        // callback when script has loaded ...
        // do something ...
        });
    });

}
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44