如何获取已加载脚本的列表,包括动态加载的脚本

发布于 12-15 07:43 字数 302 浏览 0 评论 0原文

我试图将某个脚本与页面上当前加载的脚本进行匹配:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝梦月影2024-12-22 07:43:36
var isThere = false;
$("script").each(function() {
    if ($(this).attr("src") == sn) {
        isThere = true;  
        // this works also with injected scripts!!
    }
});
if (isThere === false) {
    $('head').append('<scr' + 'ipt src="' + sn +'"></scr' + 'ipt>');
}
var isThere = false;
$("script").each(function() {
    if ($(this).attr("src") == sn) {
        isThere = true;  
        // this works also with injected scripts!!
    }
});
if (isThere === false) {
    $('head').append('<scr' + 'ipt src="' + sn +'"></scr' + 'ipt>');
}
一城柳絮吹成雪2024-12-22 07:43:36

不可能可靠地知道加载了哪些脚本元素,因为一旦加载并执行了脚本元素,就可以将其删除,而对文档的影响为零。此外,使用 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.

往事随风而去2024-12-22 07:43:36

为什么不测试脚本中是否存在某些内容(例如全局变量名称、全局对象或函数名称)。

if (!window.functionName) {
    // script holding functionName must not be present so load it dynamically
}

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).

if (!window.functionName) {
    // script holding functionName must not be present so load it dynamically
}
顾铮苏瑾2024-12-22 07:43:36

您可以检查条目数组

const entries = performance.getEntries();

此条目包含属性

// entries[0];

{
  detail: null,
  duration: 1586,
  entryType: "measure",
  name: "Next.js-before-hydration",
  startTime: 0
}

You can check array of entries

const entries = performance.getEntries();

This entry includes properties

// entries[0];

{
  detail: null,
  duration: 1586,
  entryType: "measure",
  name: "Next.js-before-hydration",
  startTime: 0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文