在我的主题目录中存在的template.php中,我有这个方法:
function vocabt_process_page(&$vars) {
drupal_flush_all_caches();
// Try to add js per page
$alias = drupal_get_path_alias($_GET['q']);
$file = $vars['directory'].'/js/'.str_replace('/', '-', $alias).'.js';
if(file_exists($file)) {
drupal_add_js($vars['directory'].'/anythingslider/anythingslider.js');
drupal_add_js($vars['directory'].'/anythingslider/jquery.anythingslider.video.min.js');
drupal_add_js($file);
}
$vars['title_sub'] = '';
$vars['page_icon'] = '';
$content_class = array();
switch($alias) {
case 'home':
drupal_add_css(drupal_get_path('theme', 'vocabt') . '/anythingslider/css/anythingslider.css');
$test = drupal_add_js(NULL, NULL, NULL);
error_log(print_r($test,1));
unset($vars['title']);
break;
//other pages
}
if(drupal_match_path($alias, 'scores/admin/*')) {
// $vars['title_sub'] = 'Check your student\'s scores by entering your information below.';
$vars['page_icon'] = 'graph';
}
if($vars['page_icon']) {
$content_class[] = 'icon-65';
}
if($vars['page']['sidebar_right']) {
$content_class[] = 'twocol';
}
$vars['content_class'] = implode(' ', $content_class);
// Since the title and the shortcut link are both block level elements,
// positioning them next to each other is much simpler with a wrapper div.
if (!empty($vars['title_suffix']['add_or_remove_shortcut']) && $vars['title']) {
// Add a wrapper div using the title_prefix and title_suffix render elements.
$vars['title_prefix']['shortcut_wrapper'] = array(
'#markup' => '<div class="shortcut-wrapper clearfix">',
'#weight' => 100,
);
$vars['title_suffix']['shortcut_wrapper'] = array(
'#markup' => '</div>',
'#weight' => -99,
);
// Make sure the shortcut link is the first item in title_suffix.
$vars['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
}
}
主要是主页加载jquery,anythingslider.js,然后是home.js
在home.js中我只是: (function($) {
$(document).ready(function() {
alert('asdf');
$('#gallery').anythingSlider();
});
})(jQuery);
我的问题是,当我加载主页时...我收到一个JS错误说:
Uncaught TypeError: Object [object Object] has no method 'anythingSlider'
这让我相信没有正确导入anythingslider库。有人能告诉我如何快速/脏地导入anythingslider.js成功吗? (记住它已经放在home.js之后)。谢谢!
答案 0 :(得分:1)
试试这些......
您可以在.info
上添加javascriptscripts[] = javascript.js
2.在template.php上添加javascript
drupal_add_js(drupal_get_path('theme', 'nameofthetheme') . '/js/jquery.js');
3.对于exrenal javascripts
drupal_add_js('http://sitename.com/javascript.js', 'external'));
4.您也可以在page.tpl.php中使用
<? php print drupal_get_js(); ?>.
您还可以通过hook_init()或hook_preprocess_page()
或hook_preprocess()
希望这可以帮助你...