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>
3
  • 1
    How are you loading it? Any reason you're not saying the script as a .js file and using jQuery's $.getScript? Commented Jun 20, 2013 at 22:43
  • You cannot append script tags as text with jQuery since it uses the .innerHTML which executes any scripts within script tags. Commented Jun 20, 2013 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... Commented Jun 21, 2013 at 13:17

1 Answer 1

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 ...
        });
    });

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.