Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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, it replaced it... 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>
share|improve this question

marked as duplicate by Marcus Ekwall, Omar, skuntsel, Rup, Roman C Jun 21 '13 at 10:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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. –  Marcus Ekwall 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 Answer 1

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

}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.