4

I'm trying to load a javascript file using jQuery's $getScript, I'm loading the script on button click. The script is supposed to append a node's content into another node, the problem is that I see the script is loaded but it's not executing.

<script type="text/javascript">
$('#but').on('click', function(){

  $.getScript('/static/39f3e10/js/wrapperloadxb.js')
    .done(function(textStatus) {
      /*optional stuff to do after getScript */
      console.log('script is loaded');
    })
    .fail(function(jqxhr, settings, exception){
      console.log("error");
    })

});


</script>

the script to load

var wrapper  = function(){
  var ul = document.getElementById('xbUL');
  var container = document.getElementById('xxx');
  container.appendChild(ul)
  };

PS: I'm adding $.getScript inside an underscore template script

1 Answer 1

4

With $.getScript you are just inserting the code onto your main page. Still you need to call the required function like the normal function call wrapper():

$('#but').on('click', function(){

  $.getScript('/static/39f3e10/js/wrapperloadxb.js')
    .done(function(textStatus) {
      /*optional stuff to do after getScript */
      console.log('script is loaded');

      //calling the wrapper function
      wrapper();
    })
    .fail(function(jqxhr, settings, exception){
      console.log("error");
    })

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

1 Comment

that's exactly what was missing, i can't believe i missed that, Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.