检查 jQuery 是否包含在 Header (Joomla) 中

2022-01-06 00:00:00 jquery php html plugins joomla

有没有办法检查是否使用 PHP 加载了 jQuery?

Is there a way to check if jQuery is loaded using PHP?

我在 Joomla 中有两个不同的插件来加载 jQuery JS,但是当它被多次包含时它不能正常工作.

I have two different plugins in Joomla that load the jQuery JS, but when it is included more than once it does not work correctly.

再解释一下这个过程:Joomla 提供了在呈现 HTML 源代码之前拦截它的能力,本质上是处理源代码本身.

To explain the process a bit more: Joomla offers an ability to intercept the HTML source before it is rendered, essentially working on the source code itself.

这是使用函数:

onPrepareContent(&$row, &$params, $limitstart)

$row 是页面可以解析的 HTML 内容.

$row is the HTML content of the page that can be parsed.

我在想也许 preg_match 可以工作,但没有太多经验.

I was thinking that maybe a preg_match could work but don't have very much experience with it.

推荐答案

更好的是,您可以使用 JavaScript 进行验证,然后在缺少时将其添加到头部.

Better yet, you can verify it with JavaScript and then add it to the head if missing.

   if (typeof jQuery == 'undefined') { 
   var head = document.getElementsByTagName("head")[0];
   script = document.createElement('script');
   script.id = 'jQuery';
   script.type = 'text/javascript';
   script.src = 'js/jquery.js';
   head.appendChild(script); 
}

相关文章