简体   繁体   中英

Fire onload event when script is loaded

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM