简体   繁体   中英

What is the right way of organizing JQuery and other plugins?

I have an HTML that uses Jquery. Let's say in the I call to them.

<head>
<script type="text/javascript" src="jquery.min.js"></script> 
<script type="text/javascript" src="jquery.jshowoff.min_130315_page.js"></script>
</head>
<body>
    <script>  </script> <!-- jquery.jshowoff plugin will be used below! -->
    <div> </div>
    <div> </div>

    <!-- And an ads extension adds a link to jquery again. -->
    <script type="text/javascript" src="jquery.min.js"></script> 

    <!-- scripts below will jquery.min.js -->


</body>

My question is. What will be the effect of this?

  1. Will calling jquery again disable the plugin (jquery.jshowoff)?
  2. Will the browser only be able to read jquery.min and not the plugin?

I hope this is clear enough if so, I can be extra clear. Thank you.

As far as I know, the second jQuery script will overwrite the first and there goes the loaded plugins too. How about if you move your plugin script tag below the ads part and erase the first jQuery tag. Then just make sure your code waits for jQuery ready event?

The effect in some cases depends on the script. In most cases merely importing a library twice won't cause any problem but it is wasteful. In other cases if a script is doing some set up then importing it twice has the following effect, the second instance of the script un-does all the work of the first instance.

In your case I guess you have no control over the 'ad extension' so you might have to control the order of the scripts to prevent conflicts and contradictory results.

As @Sanfor states pulling in these two scripts will cause the second to over-write the other. Don't forget that JS scripts are executed in order.

Have you tried putting your scripts at the bottom or at least below the 'ad extension'?

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