简体   繁体   中英

JavaScript listen for jQuery event

I'm having trouble passing an event from jQuery to Javascript. I am setting the JS event listener first and then firing the jQuery event from an external file, but the listener never gets triggerred. Here's some code:

Listener

<script type="text/javascript">
document.addEventListener('readyToPlay', loadLibs);

function loadLibs() {
    alert('success');
}

</script>
<script src="/assets/js/plugins/co/co.js" type="text/javascript"></script>

Included File - The alert returns (function), which means jQuery is loaded

function init() {
    alert(typeof jQuery);
    $(document).trigger('readyToPlay');
}

I am thinking the issue might be caused by referencing document in the external file, but I'm not sure how to get around that.

You can't use jQuery but you can fire it in Vanilla JS

function init() {
  var event= new CustomEvent('readyToPlay',[]);
  document.dispatchEvent(event);
}

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