简体   繁体   中英

How to include multiple JS files are dependent on the event onload or $(document).ready()?

There are several libraries that are in different files, some of them must be included at the same time onload event. Convenience that I can call only the library that I need. sometimes I need to block access to including file.

That's what I use:

<script type="text/javascript" src="<?=JS;?>swfupload/swfupload.js"></script>
<script type="text/javascript" src="<?=JS;?>swfupload/handlers.<?=$language;?>.js"></script>

<!-- ACTION -->
<script type="text/javascript">

  $(document).ready(function(){

      $.getScript("<?=JS;?>swfupload/swfupload.action.js");
      $.getScript("<?=JS;?>maps/gmap3.action.js");
      $.getScript("<?=JS;?>tiny_mce/tiny_mce.action.js");
      $.getScript("/jvs/cabinet.company.js");

  });

</script>

<!-- tinyMCE -->
<script type="text/javascript" src="<?=JS;?>tiny_mce/tiny_mce.js" ></script>

But I don't like using $.getScript method, some events are not processed by Ajax. Please help solve the problem.

I think you need to just use the done and fail functions, like this:

$.getScript( "ajax/test.js" )
  .done(function( script, textStatus ) {
    console.log( textStatus );
  })
  .fail(function( jqxhr, settings, exception ) {
    $( "div.log" ).text( "Triggered ajaxError handler." );
});

Or just use a standard Ajax request, like this:

$.ajax({
  url: url,
  dataType: "script",
  success: success
});

It's covered in the docs: http://api.jquery.com/jQuery.getScript/

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