0

So I have the following code:

 $.proxy(function(responseData) {
                    if (responseData.hasOwnProperty('dynamicJavascriptUrl')) {
                         var script = document.createElement('script');
                        script.src = responseData.dynamicJavascriptUrl;
                        script.load = function() {
                            console.log('hello');
                        }
                        $('#credit_card').append($(script));
                    }

And what I am trying to do here is to log hello when the script is loaded. But it doesnt seem to work.

I have tried this as well:

 script.onload = function() {
                            console.log('hello');
                        }

But with the same result.

Does anyone have an idea what to do?

  • tried this? http://stackoverflow.com/a/16231055/1984039 – gurvinder372 Jan 14 '16 at 09:07
  • It should be `script.onload` for sure, not `script.load` or binding using jQuery `$(script).on('load', handler);`. And ya, set onload event before setting src property. But wait, what is `$.proxy` here? The jQuery one doesn't pass any param to callaback: https://api.jquery.com/jQuery.proxy/ – A. Wolff Jan 14 '16 at 09:10
  • Try with https://api.jquery.com/jquery.getscript/ – Satpal Jan 14 '16 at 09:14
  • $.proxy is used here to call exsting functions that do the callback. I did it this way since it has different callbacks depending on what you need. – Patrick Ziebell Thøgersen Jan 14 '16 at 09:26

1 Answers1

2

Try This Code

jQuery.loadScript = function (url, callback) {
   jQuery.ajax({
      url: url,
      dataType: 'script',
      success: callback,
      async: true
   });
}

$.loadScript('dynamicJavascriptUrl.js', function(){
    console.log('hello');
});