2016-05-13 92 views
1

在此先感謝您的幫助。我很難保持我的代碼庫清潔。我想避免混用PHP,HTML和CSS。動態地包含來自外部文件的JavaScript - PHP

目前,我的主站點被分解成許多較小的選項卡。在進行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>"; 

這個網站是一個單頁的應用程序。再次感謝!

回答

1

只是print<script>標籤,包括它:

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

Javascript函數不能包含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 } ?> 
+0

謝謝你的評論,但你能解釋一下這個更詳細幹什麼? – linuxisthebest33

+0

以上代碼用於根據您的情況在php文件中包含JavaScript文件。包含js文件的代碼與包含php文件不一樣。 –

+0

感謝您的幫助! – linuxisthebest33

0

喜在我來說,我使用seprated較小parts.i模塊基本模板有3個主要的UI部分我網站 1.所有模板jquery,bootstrap,...在所有模板中使用的公共js必須放在這裏 2.每個樣式或模板都有一個js文件夾,該模板的所有公共js文件必須存在 3。模板中的每個模塊都有js文件夾,該文件夾必須是該模塊特有的js文件夾有 我這樣做對CSS too.in事實上,當我通過

array_slice(scandir($st_css_style_public_path), 2) 

加載模塊檢查所有這些文件夾,並創建CSS鏈接或JS腳本並打印地址的最後串在我的網頁。 但有些時候你需要注入的代碼和平直接到您的網頁,我使用的文件夾,並用插件 - > 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); 
     } 
    } 

我用樹枝模板引擎所以有一些不相關的代碼,在這裏你的問題,其他部分是Ajax調用。 結論: 1 - 您可以使用此結構來添加或刪除模塊中的文件,就像從文件夾中複製或刪除文件一樣簡單。 2 - 你可以用它來創建正確的JS或CSS通過Ajax創建的地址,並打印在你的代碼

我希望它幫你,不要猶豫,因爲如果你需要

+0

感謝您的幫助! – linuxisthebest33

0

PHP更多的問題include()是服務器端。

JavaScript是客戶端。

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

不過,如果你想加載一個JavaScript與你想有一個網址,使用: $url = "JAVASCRIPT URL HERE"; echo('<script src="'. $url .'"></script>');

相關問題