简体   繁体   中英

console.log doesn't execute within callback but alert() does?

I'm scratching my head rather heavily over this one. So I have a JS script that needs to execute after another script so I've injected it into the head with a callback. Here's a snippet of the code that works, it pops up with the alert after loading the initial script.

    loadBackupScript(function() { 
    alert('test'); 
});

But merely changing the alert to console.log (see below) seems to break the functionality, it doesn't throw up any errors, just fails to log anything in the console. Anything more complex within the anonymous function appears to do the same. Any ideas?

    loadBackupScript(function() { 
    console.log('test'); 
});

Edit: Here's the loadBackupScript function, but I'm certain it's not a problem with this as it executes an alert perfectly fine.

function loadBackupScript(callback) {
  var script;
   if (typeof callback !== 'function') {
      throw new Error('Not a valid callback');  
   }
   script = document.createElement('script');
   script.onload = callback; 
   script.src = '/SiteAssets/Kent Normal1.js';
   document.head.appendChild(script);
}

我错误地单击了firebug中的日志记录选项,感谢synaps的评论,在进入这里之前应该已经对其进行了真正的测试。

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