Sep*_*ram 4 javascript requirejs
只需使用<script />标记加载脚本,就可以检索脚本URL,如下所示:
var scriptURL = Array.prototype.pop.call ( // get the last...
document.getElementsByTagName ('script') // ...script tag on the page...
).src.split ('?') [0]; // ...take 'src' attribute...
// ...and strip the query string
Run Code Online (Sandbox Code Playgroud)
这有点像黑客,但有时可能非常有用,原因有很多(例如,当有其他资源文件依赖于脚本并且您不想对路径进行硬编码时).它的工作原理是因为在执行时<script />页面上存在的最后一个标记就是你的脚本.
我不确定在使用RequireJS加载脚本时是否如此.RequireJS中是否有类似的方法从模块定义中检索脚本URL?
您可以要求module通常用于传递特殊config设置的模块:
define(['module'], function (module) {
console.log(module)
}
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个对象,其中包含id和uri到模块
| 归档时间: |
|
| 查看次数: |
4400 次 |
| 最近记录: |