如何获取已加载脚本的列表,包括动态加载的脚本
我试图将某个脚本与页面上当前加载的脚本进行匹配:
if($('script[src="' + sn + '"]').length == 0)
$('head').append('<scr' + 'ipt src="' + sn +'"></scr' + 'ipt>');
这有效,但仅适用于正常加载的脚本。
对于使用 javascript 注入的脚本,就像我的情况一样,它不起作用:(
显然,以这种方式加载的脚本在 DOM 中以某种方式不可见,即使它们运行。
I'm trying to match a certain script with the currently loaded scripts on the page:
if($('script[src="' + sn + '"]').length == 0)
$('head').append('<scr' + 'ipt src="' + sn +'"></scr' + 'ipt>');
This works but only for scripts that were loaded normally.
For scripts that were injected using javascript, like the one in my condition, it doesn't work :(
Apparently scripts that are loaded this way are somehow invisible in the DOM, even though they run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不可能可靠地知道加载了哪些脚本元素,因为一旦加载并执行了脚本元素,就可以将其删除,而对文档的影响为零。此外,使用 innerHTML 属性插入的脚本元素不会被执行,因此它们的存在毫无意义(或多或少)。
您所能做的就是获取文档中当前脚本元素的列表,您无法可靠地知道哪些脚本元素已被加载甚至执行。
It is impossible to know reliably what script elements have been loaded because once a script element is loaded and executed, it can be removed with zero effect on the document. Also, script elements inserted usig the innerHTML property don't get executed, so their presence is meaningless (more or less).
All you can do is get a list of the current script elements in the document, you can't reliably know which ones have been loaded or even executed.
为什么不测试脚本中是否存在某些内容(例如全局变量名称、全局对象或函数名称)。
Why don't you test for the existence of something in your script (such as a global variable name, a global object or a function name).
嘿,您可以使用 PerformanceAPI:
https://developer.mozilla .org/en-US/docs/Web/API/Performance/getEntries
Hey you can use the PerformanceAPI:
https://developer.mozilla.org/en-US/docs/Web/API/Performance/getEntries
您可以检查条目数组
此条目包含属性
You can check array of entries
This entry includes properties