Use javascript to inject script references as needed?

后端 未结 6 843
终归单人心
终归单人心 2020-12-08 03:20

I have a JS function that may occasionally get used on some pages. It is dependent on another JS file (swfObject.js), but I\'d like to avoid having to include this file all

6条回答
  •  情话喂你
    2020-12-08 04:05

    Consider using require.js. This might need some rework of your frontend framework, but it's totally worth it. With require, you could just do the following in your fileUsedOccasionally.js:

    define(['swfObject', 'someOtherDependency'], function (swfObject, someOtherDependency) {
      // you can now use swfObject as a JS object! you can call it whatever you want
      // you'll have to write a swfObject.js to wrap it with require
      // but that's trivial
    });
    

提交回复
热议问题