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

时间:2021-10-13 00:15:42

Thanks in advance for your help. I am having a hard time keeping my codebase clean. I want to avoid intermixing PHP, HTML, and CSS.

在此先感谢您的帮助。我很难保持代码库的清洁。我想避免混用PHP,HTML和CSS。

Currently, my main site is broken down into numerous smaller tabs. The PHP code for these tabs is dynamically included after an ajax call is made.

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

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

It works great but I would also like to dynamically include JavaScript from an external file. In my mind it would work like this,

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

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

How can I dynamically include javascript based on what tab the user wants to go to while still keeping the javascript separated by tab in individual files.

如何根据用户想要访问的选项卡动态包含javascript,同时仍然将javascript分隔为单个文件中的选项卡。

I have spent the entire day looking into this issue and keep coming across examples that look like this,

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

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

This site is a single page application. THanks again!

该站点是单页面应用程序。再次感谢!

4 个解决方案

#1


1  

Just print the <script> tag to include it:

只需打印

 print '<script src="templates/custom/'.$center.'/'.$section.'/'.$tab.'.js'" type="text/javascript"></script>';

#2


1  

Javascript files cannot be included by php function. Use the below code

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 } ?>

#3


0  

hi in my case i use module base template that seprated to smaller parts.i have 3 main UI part in my site 1.public site js for all templates jquery,bootstrap ,... that use in all templates must put here 2.each style or template has a js folder that all public js file of this templates must be there 3.each module in template has js folder that js special for that module must be there i do it for css too.in fact when i load a module check all of this folders by

嗨,在我的情况下,我使用模块基础模板,分离到较小的部分。我有3个主要的UI部分在我的网站1.public网站js的所有模板jquery,bootstrap,...在所有模板中使用必须放在这里2.each样式或模板有一个js文件夹,该模板的所有公共js文件必须在那里3.模板中的每个模块都有js文件夹,该模块的js特殊必须在那里我也为css做了。事实上当我加载一个模块检查所有这些文件夹

array_slice(scandir($st_css_style_public_path), 2)

and create css link or js script and print final string of addresses in my page. but some times you need to inject a peace of code directly into your page i use a folder and a file with name of plugins->plugins.php put all piece of script there get it's content and print it into my page

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

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

all of my render method in my view is this :

我视图中的所有渲染方法都是这样的:

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);
        }
    }

i am using twig template engine so there are some unrelated code to your question here;else part is for ajax call. conclusion: 1-you can use this structure to add or delete file from a module as easy as copy or delete a file from it's folder. 2- you can use it to create correct js or css to create address by ajax and print it in your code

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

i hope it helped you and don't hesitate to as more question if you need

我希望它对你有所帮助,如果你需要,可以毫不犹豫地提出更多问题

#4


0  

PHP include()'s are server-side.

PHP include()是服务器端的。

JavaScript is client-side.

JavaScript是客户端的。

Therefore, you cannot use include() on a JavaScript.

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

However, if you would like to load a JavaScript with a URL that you want, use this: $url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');

但是,如果您要加载带有所需URL的JavaScript,请使用以下命令:$ url =“JAVASCRIPT URL HERE”; echo('

#1


1  

Just print the <script> tag to include it:

只需打印

 print '<script src="templates/custom/'.$center.'/'.$section.'/'.$tab.'.js'" type="text/javascript"></script>';

#2


1  

Javascript files cannot be included by php function. Use the below code

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 } ?>

#3


0  

hi in my case i use module base template that seprated to smaller parts.i have 3 main UI part in my site 1.public site js for all templates jquery,bootstrap ,... that use in all templates must put here 2.each style or template has a js folder that all public js file of this templates must be there 3.each module in template has js folder that js special for that module must be there i do it for css too.in fact when i load a module check all of this folders by

嗨,在我的情况下,我使用模块基础模板,分离到较小的部分。我有3个主要的UI部分在我的网站1.public网站js的所有模板jquery,bootstrap,...在所有模板中使用必须放在这里2.each样式或模板有一个js文件夹,该模板的所有公共js文件必须在那里3.模板中的每个模块都有js文件夹,该模块的js特殊必须在那里我也为css做了。事实上当我加载一个模块检查所有这些文件夹

array_slice(scandir($st_css_style_public_path), 2)

and create css link or js script and print final string of addresses in my page. but some times you need to inject a peace of code directly into your page i use a folder and a file with name of plugins->plugins.php put all piece of script there get it's content and print it into my page

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

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

all of my render method in my view is this :

我视图中的所有渲染方法都是这样的:

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);
        }
    }

i am using twig template engine so there are some unrelated code to your question here;else part is for ajax call. conclusion: 1-you can use this structure to add or delete file from a module as easy as copy or delete a file from it's folder. 2- you can use it to create correct js or css to create address by ajax and print it in your code

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

i hope it helped you and don't hesitate to as more question if you need

我希望它对你有所帮助,如果你需要,可以毫不犹豫地提出更多问题

#4


0  

PHP include()'s are server-side.

PHP include()是服务器端的。

JavaScript is client-side.

JavaScript是客户端的。

Therefore, you cannot use include() on a JavaScript.

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

However, if you would like to load a JavaScript with a URL that you want, use this: $url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');

但是,如果您要加载带有所需URL的JavaScript,请使用以下命令:$ url =“JAVASCRIPT URL HERE”; echo('