简体   繁体   English

如果未添加PHP和Javascript,则添加样式表/脚本

[英]Add stylesheet/script if not added PHP & Javascript

I don't even know where to begin attempting this, what I'm trying to do is only load a file once, since I have multiple instances of it currently. 我什至不知道从哪里开始尝试此操作,由于当前我有多个文件实例,因此我试图做的只是将其加载一次。

I load the files in with php, the if conditions are just whether the option is on or another. 我用php加载文件,如果条件只是选项是否打开。

if ($vbulletin->options['drc_fa_adm'] & $vbulletin->options['drc_fa_prov'] != 0){
  if ($vbulletin->options['drc_fa_prov'] == 1) {
    echo '<link rel="stylesheet" href="'.$vbulletin->options['bburl'].'/drc/dirty-core/libs/font-awesome/css/font-awesome.min.css">';
  }
  if ($vbulletin->options['drc_fa_prov'] == 2) {
    echo '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">';
  }
}

if ($vbulletin->options['drc_jq_prov'] == 1) {
  echo '<script src="'.$vbulletin->options['bburl'].'/drc/dirty-core/libs/jquery/jquery.min.js"></script>';
} else {
  echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>';
}

echo '<script src="../drc/dirty-core/libs/jscolor/jscolor.min.js"></script><script src="../drc/dirty-core/js/twit_adm.js"></script>';

this all hooks into a specific hook that some pages (not all) have multiple instances of, which cause all of them to be added 2,3 or more times. 所有这些钩子都插入到特定的钩子中,某些页面(不是全部)具有多个实例,这会导致所有它们被添加2,3或更多次。

How can I check to see if they're already on the page, and if they are to not echo them again? 如何检查它们是否已经在页面上,以及是否不再回显它们?

I think something like this could work I just don't know how I can add it to my PHP 我认为类似这样的方法可能有效,只是我不知道如何将其添加到PHP中

<script type="text/javascript">
  if(typeof jQuery == 'undefined'){
    document.write('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></'+'script>');
  }
</script>

Try this: 尝试这个:

$alreadyLoaded = array('css' => false, 'jquery' => false);
if ($vbulletin->options['drc_fa_adm'] & $vbulletin->options['drc_fa_prov'] != 0){
  if ($vbulletin->options['drc_fa_prov'] == 1 and !$alreadyLoaded['css']) {
    echo '<link rel="stylesheet" href="'.$vbulletin->options['bburl'] .
    '/drc/dirty-core/libs/font-awesome/css/font-awesome.min.css">';
    $alreadyLoaded['css'] = true;
}
if ($vbulletin->options['drc_fa_prov'] == 2 and !$alreadyLoaded['css']) {
    echo '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">';
    $alreadyLoaded['css'] = true;
    }
}

if ($vbulletin->options['drc_jq_prov'] == 1 and !$alreadyLoaded['jquery']) {
    echo '<script src="'.$vbulletin->options['bburl'].'/drc/dirty-core/libs/jquery/jquery.min.js"></script>';
    $alreadyLoaded['jquery'] = true;
} else if (!$alreadyLoaded['jquery']) {
    echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>';
    $alreadyLoaded['jquery'] = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM