Hi All,
I searched everywhere and could not get an answer. I am new to Drupal and I am trying to add a little bit of javascript to the content that appears in the "content" block on the front page of my website. It is basically a simple script that loads a random video (contained in an array) off a streaming video site.
So I added the file to the folder: sites/all/themes/marinellisub/js/front.js
I tested the the script in a non drupal environment and it does work as it is supposed to.
Now I tried adding the Below line to two templates: node--1.tpl.php(the node I'd like to add the content to) AND page--front.tpl.php(the front page).
<?php
drupal_add_js('js/front.js', array('type' => 'file', 'scope'=>'content'));
?>I manually add the code through the content editor manually by adding "" tags, wwhich also did not work. I am totally at a loss at how to accomplish this.
Any input is appreciated and please let me know if you would like me to provide more information.
FYI I am using Drupal 7x.
Thanks in advance for any help!
Dan
Comments
you need to add the code in
you need to add the code in template.php under preprocess_page function
eg
<?phpmythemename_preprocess_page(&$variables) {
if (drupal_is_front_page()) {
drupal_add_js(......);
}
}
?>
then clear cache.
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com - Premium Theme Club
skype id : duckzland
Didn't seem to work...
Hi duckzland
Thanks for the help! Unfortunately this didn't work for me. Maybe I messed something up somewhere? I added the code you recommended to this block in the template.php file:
function marinelli_preprocess_page(&$vars) {
// Useful for devel default banners, remove before commit
// variable_del('theme_marinelli_first_install');
// Chcek if is first setup of marinelli and install banners.
if (variable_get('theme_marinelli_first_install', TRUE)) {
include_once('theme-settings.php');
_marinelli_install();
}
if (drupal_is_front_page()) {
drupal_add_js('js/front.js', array('type' => 'file', 'scope'=>'content'));
}
Daniel Miller
you need to give the correct
you need to give the correct path 'js/front.js' = http://somedomain.com/js/front.js.
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com - Premium Theme Club
skype id : duckzland
path
Duckzland
This might be a silly question, but the file is on my localhost environment located in: "sites/all/themes/marinellisub/js" folder. Just to be safe I also put it in the "sites/all/themes/marinelli/js" folder just to make sure(the subtheme seems to take some of the info from the parent theme. In this situation how would I reference the file?
Thanks!
Daniel Miller
Another example
Another example of the syntax for your template.php fil
function marinelli_preprocess_page(&$variables) {drupal_add_js(drupal_get_path('theme', 'marinelli') .'/js/front.js', 'file');
}
If it's included on every page then it's better to include it in your theme's .info file as it gets added to the theme registry.
scripts[] = js/front.js
re
Hi edrupal
I only need to use the javascript on the front page actually. I did try adding your code and it is still not loading on my front page. I am trying to add it just on the main content area. I am not sure what is going on or if it has something to do with the theme i'm using.
THanks for the help!
Dan
Daniel Miller
Hi, Put back your js in your
Hi,
Put back your js in your subtheme folder (Assuming you are putting it @ this path sites/all/themes/marinellisub/js/front.js and marinellisub is the current enabled theme). Then in template.php of this theme put this,
function marinellisub_preprocess_page(&$variables, $hook) {if (drupal_is_front_page()) {
drupal_add_js(path_to_theme() . '/js/front.js', array('type' => 'file', 'scope'=>'content'));
}
}
Clear cache and check.
Hope this help !
Prajakta