[英]Dynamically include JavaScript from external file - PHP
在此先感谢您的帮助。 我很难保持代码库整洁。 我想避免PHP,HTML和CSS混杂在一起。
目前,我的主站点分为多个较小的选项卡。 进行ajax调用后,将动态包含这些选项卡的PHP代码。
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>";
该站点是单页应用程序。 再次感谢!
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 } ?>
只需print
<script>
标记以包括它:
print '<script src="templates/custom/'.$center.'/'.$section.'/'.$tab.'.js'" type="text/javascript"></script>';
您好,在我的情况下,我使用模块基础模板,这些模板分为较小的部分。我在我的网站中有3个主要的UI部分。1。所有模板的jquery,bootstrap等公共模板都必须放在这里。样式或模板具有一个js文件夹,该模板的所有公共js文件都必须在该文件夹中。3。模板中的每个模块都具有该模块专用js的js文件夹,我也必须在CSS中执行该操作。实际上,当我加载模块时检查所有此文件夹
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) : ' ';
在我看来,我所有的render方法是这样的:
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);
}
}
我正在使用树枝模板引擎,所以这里有一些与您的问题无关的代码;其他部分是针对ajax调用的。 结论:1-您可以使用此结构从模块中添加或删除文件,就像从其文件夹中复制或删除文件一样容易。 2-您可以使用它来创建正确的js或css,以通过ajax创建地址并将其打印在代码中
我希望它能对您有所帮助,如果您需要,请不要犹豫,提出更多问题
PHP include()在服务器端。
JavaScript是客户端。
因此,不能在JavaScript上使用include()。
但是,如果您想用所需的URL加载JavaScript,请使用以下命令: $url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');
$url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.