动态地包含来自外部文件PHP的JavaScript

Dynamically include JavaScript from external file - PHP

本文关键字:文件 PHP JavaScript 外部 包含 动态      更新时间:2023-09-26

提前感谢您的帮助。我很难保持我的代码库干净。我希望避免混合PHP、HTML和CSS。

目前,我的主站点被分解成许多较小的选项卡。这些选项卡的PHP代码是在进行ajax调用后动态包含的。

elseif (file_exists('templates/custom/'.$center."/".$section."/".$tab.".php")) {
 include 'templates/custom/'.$center."/".$section."/".$tab.".php";
 }

它工作得很好,但我也想从外部文件动态地包含JavaScript。在我看来,它会这样工作,

elseif (file_exists('templates/custom/'.$center."/".$section."/".$tab.".php")) {
 include 'templates/custom/'.$center."/".$section."/".$tab.".php";
 include 'templates/custom/'.$center."/".$section."/".$tab.".js";
 }

如何根据用户想要转到的选项卡动态包含javascript,同时在各个文件中保持javascript按选项卡分隔

我花了一整天的时间研究这个问题,并不断遇到这样的例子,

echo "<script language='javascript' type='text/javascript'>";
echo "alert('hello worldio');";
echo "</script>";
$URL="page.php";
echo "<script>location.href='$URL'</script>";

此网站是一个单页应用程序。再次握手!

print <script>标签即可包含它:

 print '<script src="templates/custom/'.$center.'/'.$section.'/'.$tab.'.js'" type="text/javascript"></script>';
php函数不能包含

Javascript文件。使用以下代码

elseif (file_exists('templates/custom/'.$center."/".$section."/".$tab.".php")) {
 include 'templates/custom/'.$center."/".$section."/".$tab.".php"; 
 $file_path = "javascript external file path"; // replace with correct file path
?>
 <script language="JavaScript" type="text/javascript" src="<?php echo $file_path;?>"></script>
 <?php } ?>

hi在我的案例中,我使用的是模块基础模板,它被划分为较小的部分。我的网站中有3个主要的UI部分1.所有模板的公共站点js jquery,bootstrap,。。。所有模板中使用的必须放在此处2.每个样式或模板都有一个js文件夹,该模板的所有公共js文件都必须在那里3.模板中的每个模块都有js文件夹,该模块的js专用文件夹必须在那里事实上,当我加载一个模块时,通过检查所有这些文件夹

array_slice(scandir($st_css_style_public_path), 2)

创建css链接或js脚本,并在我的页面中打印最后一个地址字符串。但有时你需要直接在你的页面中注入代码,我使用一个文件夹和一个名为plugins->plugins.php的文件,把所有的脚本都放在那里,获取内容并打印到我的页面中

`$st_plugins .= (file_exists($st_plugin_style_public_path) ) ? file_get_contents($st_plugin_style_public_path) : ' ';

在我看来,我所有的渲染方法都是这样的:

public function render($address, $data = '', $cache = 1, $showstyle = 1) {
        $data['LINKPREFIX'] = '/' . $this->current_holding_unique_name
                . '/'
                . $this->current_lang;
        if (isset($address)) {
            $path = explode('/', $address);
            $path[0] = $path[0];
            $path[1] = $path[1];
        }
        $template = $this->twig->loadTemplate($path[0] . DS . $path[1] . '.twig');
        if ($showstyle) {
            $css_links = '';
            $js_links = '';
            $st_plugins = '';
            //##################################################
            //########################## CREATING CSS,JS ADDRESS
            //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            //####### SITE PUBLIC CSS & JS FILES
            $st_js_public_path = '.' . DS . PUBLIC_DIR . DS . $this->set_address($path[0]) . 'js';
            $st_css_public_path = '.' . DS . PUBLIC_DIR . DS . $this->set_address($path[0]) . 'css';
            if (file_exists($st_js_public_path) && is_dir($st_js_public_path)) {
                $ar_public_jsfile_list = array_slice(scandir($st_js_public_path), 2);
                foreach ($ar_public_jsfile_list as $js_file_name) {
                    $js_links .= $this->create_css_js_link($st_js_public_path . DS . $js_file_name, 'js');
                }
            }
            if (file_exists($st_css_public_path) && is_dir($st_css_public_path)) {
                $ar_public_cssfile_list = array_slice(scandir($st_css_public_path), 2);
                foreach ($ar_public_cssfile_list as $css_file_name) {
                    $css_links .= $this->create_css_js_link($st_css_public_path . DS . $css_file_name, 'css');
                }
            }
            //####### STYLE PUBLIC CSS & JS & PLUGINS FILES
            $st_js_style_public_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . 'public' . DS . $this->current_direction . DS . 'js';
            $st_css_style_public_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . 'public' . DS . $this->current_direction . DS . 'css';
            $st_plugin_style_public_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . 'public' . DS . $this->current_direction . DS . 'plugins' . DS . 'plugins.php';
            if (file_exists($st_css_style_public_path) && is_dir($st_css_style_public_path)) {
                $ar_cssfile_list = array_slice(scandir($st_css_style_public_path), 2);
                foreach ($ar_cssfile_list as $css_file_name) {
                    $css_links .= $this->create_css_js_link($st_css_style_public_path . DS . $css_file_name, 'css');
                }
            }
            if (file_exists($st_js_style_public_path) && is_dir($st_js_style_public_path)) {
                $ar_jsfile_list = array_slice(scandir($st_js_style_public_path), 2);
                foreach ($ar_jsfile_list as $js_file_name) {
                    $js_links .= $this->create_css_js_link($st_js_style_public_path . DS . $js_file_name, 'js');
                }
            }
            $st_plugins .= (file_exists($st_plugin_style_public_path) ) ? file_get_contents($st_plugin_style_public_path) : ' ';
            //####### MODULE CSS & JS FILES
            $st_js_style_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . $path[0] . DS . $this->current_direction . DS . 'js';
            $st_css_style_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . $path[0] . DS . $this->current_direction . DS . 'css';
            $st_plugin_path = '.' . DS . VIEW_DIR . DS . $this->current_style . DS . $path[0] . DS . $this->current_direction . DS . 'plugins' . DS . 'plugins.php';
            if (file_exists($st_css_style_path) && is_dir($st_css_style_path)) {
                $ar_cssfile_list = array_slice(scandir($st_css_style_path), 2);
                foreach ($ar_cssfile_list as $css_file_name) {
                    $css_links .= $this->create_css_js_link($st_css_style_path . DS . $css_file_name, 'css');
                }
            }
            if (file_exists($st_js_style_path) && is_dir($st_js_style_path)) {
                $ar_jsfile_list = array_slice(scandir($st_js_style_path), 2);
                foreach ($ar_jsfile_list as $js_file_name) {
                    $js_links .= $this->create_css_js_link($st_js_style_path . DS . $js_file_name, 'js');
                }
            }
            $st_plugins .= (file_exists($st_plugin_path) && $showstyle ) ? file_get_contents($st_plugin_path) : ' ';
            //################################################
            //################################################
            //################################################
            //################################################
            //@ @   @ CREATING CSS,JS ADDRESS
            $data['VARCSSADDR'] = $css_links;
            $data['VARJSADDR'] = $js_links . $st_plugins;
            $data['VARURL'] = '/';
            $data = array_merge($data, lang_translate::$lang);
            $template->display($data);
        } else {
            //$ar_langpropr = language::$ar_lanuage[session::get('current_lang')];
            //$data['lang_code'] = $ar_langpropr['lang_code'];
            $data = array_merge($data, lang_translate::$lang);
            return $this->twig->render($address . '.twig', $data);
        }
    }

我使用的是trick模板引擎,所以这里有一些与你的问题无关的代码;其他部分用于ajax调用。结论:1您可以使用此结构从模块中添加或删除文件,就像从其文件夹中复制或删除文件一样简单。2-您可以使用它创建正确的js或css,通过ajax创建地址,并将其打印在代码中

我希望它能帮助你,如果你需要

,请不要犹豫

PHP include()是服务器端的。

JavaScript是客户端。

因此,不能在JavaScript上使用include()。

但是,如果您想加载带有所需URL的JavaScript,请使用以下方法: $url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');