load js files for special page in template directory of zencart

How to load javascript files for a special page,for eaxmple product_info, in zencart? Of course you can use default js loading way,in common/html_header.php.

The default code is below:

/**
* load all page-specific jscript_*.js files from includes/modules/pages/PAGENAME
*/
  $directory_array = $template->get_template_part($page_directory, ‘/^jscript_/’, ‘.js’);
  while(list ($key, $value) = each($directory_array)) {
    echo ‘<script type="text/javascript" src="’ . $page_directory. ‘/’ . $value . ‘"></script>’ . "\n";
  }

For eample if you put a product_info.js in includes/modules/pages/product_info/ ,you will get the html on the goods page:

<script type="text/javascript" src="includes/modules/pages/product_info/product_info.js"></script>

Obviously the js directory is under includes\modules,and your js files is too far away from your zencart template directory.

Is there any other ways to load js files just in includes/templates/YOURTEMPLATE/jsdir?

The answer is YES.Here feilong wrote two snippets to load js files for special page just in your template directory of zencart,not in modules directory as the default loading way.

ONE, to get special js files named according pageurl in template

Load js files in includes/templates/YOURTEMPLATE/js/pagename.js for per-page/per-language/per-product/per-manufacturer/per-category basis.

In fact it is the same way as how to define zencart css files according the pageurl feilong posted last days.

The code is below:

// feilong.org: load special js files includes/templates/YOURTEMPLATE/js * for per-page/per-language/per-product/per-manufacturer/per-category
  $manufacturers_id = (isset($_GET[‘manufacturers_id’])) ? $_GET[‘manufacturers_id’] : ”;
  $tmp_products_id = (isset($_GET[‘products_id’])) ? (int)$_GET[‘products_id’] : ”;
  if ($current_page_base == ‘page’ && isset($ezpage_id)) $tmp_pagename = $current_page_base . (int)$ezpage_id;
  $sheets_array = array(‘/’ . $_SESSION[‘language’] . ‘_javascript’,
                        ‘/’ . $tmp_pagename,
                        ‘/’ . $_SESSION[‘language’] . ‘_’ . $tmp_pagename,
                        ‘/c_’ . $cPath,
                        ‘/’ . $_SESSION[‘language’] . ‘_c_’ . $cPath,
                        ‘/m_’ . $manufacturers_id,
                        ‘/’ . $_SESSION[‘language’] . ‘_m_’ . (int)$manufacturers_id,
                        ‘/p_’ . $tmp_products_id,
                        ‘/’ . $_SESSION[‘language’] . ‘_p_’ . $tmp_products_id
                        );
  while(list ($key, $value) = each($sheets_array)) {
    //echo "<!–looking for: $value–>\n";
    $perpagefile = $template->get_template_dir(‘.js’, DIR_WS_TEMPLATE, $current_page_base, ‘blue/js’) . $value . ‘.js’; //feilong.org:’blue/js’ is the directory where js files are placed
    if (file_exists($perpagefile)) echo ‘<script type="text/javascript" src="’ . $perpagefile .’" ></script>’."\n";
  }

If you put a file named product_info.js, and  a file named p_127.js, in ‘yourtemplate/blue-css/js’ directory, the result on /index.php?main_page=product_info&cPath=*&products_id=127 would be:

<script type="text/javascript" src="includes/templates/feilongtemplate/blue/js/product_info.js" ></script>
<script type="text/javascript" src="includes/templates/feilongtemplate/blue/js/p_127.js" ></script>

TWO, to load js files in the directory only for jsfiles on product_info in zencart template

The code above cat get js files for every page.BUT If you want to designate a directory to contain ONLY js files on products_ids? And load them when the current pageurl is /index.php?main_page=product_info&cPath=*&products_id=*

The code is below:

//load js fles on goods info pages includes/templates/YOURTEMPLATE/dir-only-for-jsfiles-on-product-infos/
$tmp_products_id = $_GET[‘products_id’];if($tmp_products_id) {
$directory_array = $template->get_template_part($template->get_template_dir(‘.js’,DIR_WS_TEMPLATE, $current_page_base,’blue/jsgoods’), ‘/^goods/’, ‘.js’);//feilong.org:’blue/jsgoods’ is the directory only for js on product_info
while(list ($key, $value) = each($directory_array)) {
echo ‘<script type="text/javascript" src="’ . $template->get_template_dir(‘.js’,DIR_WS_TEMPLATE, $current_page_base,’blue/jsgoods’) . ‘/’ . $value . ‘" ></script>’."\n";
}
}

If you put a file with a prefix goods,for example goods_fl.js in ‘blue/jsgoods’,the html output would be:

<script type="text/javascript" src="includes/templates/feilongtemplate/blue/jsgoods/goods_fl.js" ></script>

THREE,load js files only for  homepage of zencart

But how to load js files only for  homepage of zencart?please refer to http://feilong.org/load-javascripts-only-homepage-zencart

Ok,enjoy js files on special pages as you like as feilong!

发表评论

电子邮件地址不会被公开。 必填项已用*标注