欢迎各位兄弟 发布技术文章
这里的技术是共享的
google "drupal add js only one page"
最方便的方法还是使用 drupal_add_js
drupal_add_css 有大用
function MYTHEME_preprocess_node($vars) {
if (drupal_get_path_alias("node/{$vars['#node']->nid}") == 'foo') {
//这是增加 js
drupal_add_js(drupal_get_path('theme', 'MYTHEME') . "/js/foo.js");
$vars['scripts'] = drupal_get_js(); //最好还是加上这一句
}
if (drupal_get_path_alias("node/{$vars['#node']->nid}") == 'foo') {
drupal_add_css(drupal_get_path('theme', 'MYTHEME') . "/css/foo.css"); }
}
来自 http://drupal.stackexchange.com/questions/126/any-way-to-add-css-for-a-single-page-node
First, you most likely don't
Jaypan commented
First, you most likely don't want raphael.js being included in your .info file, as it will then be included on every page load. Unless you are using it on every page load, this is overkill. Therefore you should include it at the same time, in the same manner, as rotate.js. And on that note, I'd use this:
<?php
function my_module_node_view($node, $viewmode, $langcode)
{
if($node->type == 'article')
{
$node->content['#attached']['js'][] = array
(
'type' => 'file',
'data' => drupal_get_path('module', 'my_module') . 'js/raphael.js',
);
$node->content['#attached']['js'][] = array
(
'type' => 'file',
'data' => drupal_get_path('module', 'my_module') . 'js/rotate.js',
);
}
}
?>
But please note that this will add the file to every article node. If you want to add it only for a specific article, then you need to get the nid of that article, and instead of checking $node->type, you would check $node->nid.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Another option
DeNelo commented
Or, you can add the javascript like this in your module:
if ($node->nid == 684) { //Looking for the specific node
drupal_add_js(drupal_get_path('module', 'my_module') . '/js/myscript.js');
}
For some reason,
$node->content['#attached']['js']
didn't work for me.
What hook were you adding it
Jaypan commented
What hook were you adding it in? Also, are you using D7 or a different version?
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Two more things: 1) In
Jaypan commented
Two more things:
1) In Drupal, you do not use window.onload, you use Drupal.behaviors. Read more about this, and the Drupal 7 JavaScript API here:https://drupal.org/node/751744
2) You can wrap your code in <code></code> tags, making it easier to read. If it's PHP, you can wrap your code in <?php ?> tags.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
If this is your actual
Jaypan commented
If this is your actual code:
<?php
function my_module_node_view($node, $viewmode, $langcode)
?>
Then you will need to change 'my_module' to the machine name of your theme. You will also need to clear your cache.
I would also suggest changing this:
(function ($) {
Drupal.behaviors.my_module = {
attach: function () {
To this:
(function ($) {
Drupal.behaviors.my_module = {
attach: function () {
alert("script loaded");
This will let you know if your script has properly loaded, helping you figure out where you need to debug (if this alert does not appear, then the script is not loaded).
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Hi, i wanna add a js file to
dagoodgamez commented
Hi, i wanna add a js file to a view page. I tried using the last example you show. I created a new module, and included
<?php
function my_theme_preprocess_views_view(&$vars)
{
if($vars['view']->name == 'my_view' && $vars['view']->current_display == 'page_1')
{
$vars['view']['#attached']['js'][] = array
(
'type' => 'file',
'data' => drupal_get_path('theme', 'my_theme') . '/my_script.js',
);
}
}
?>
in the my_module.module file, but it doesnt work. Could you help me to get this work?
Thanks in advance!
You'll need to change the
Jaypan commented
You'll need to change the following:
my_theme => Machine name of your module
my_view => Machine name of your view
page_1 => Machine name of the display of the view you want to show
drupal_get_path('theme', 'my_theme') => drupal_get_path('module', MODULE MACHINE NAME) (change MODULE_MACHINE NAME)
my_script.js => name of your script
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Yes, i already did that, i
dagoodgamez commented
Yes, i already did that, i was showing the above code just as an example. But it's still not working.
Must be something else wrong
Jaypan commented
Must be something else wrong with your code then.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
I think i know what is wrong.
nagy.balint commented
I think i know what is wrong.
$vars['view'] is an object.
Yet later in the code the attach is done by
$vars['view']['#attached']
Which throws PHP Fatal error: Cannot use object of type view as array
Re:Help with adding JavaScript to one page.
drupsforyou commented
Well, you can add your js path to the .info file.
Ex : scripts[] = js/yourJS.js
You have to write your code in a such a way, so that your function will work as per your need.
That will add the script to
Jaypan commented
That will add the script to every page. This user wants to attach the scripts to a single page.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Re:; Reply to comment
drupsforyou commented
Then try to use the below function.
drupal_add_js("PATH OF YOUR JS FILE")
Try this
badger_fruit commented
Use this to "include" the .JS file into your page--xxxx.tpl.php
drupal_add_js('sites/all/themes/yourtheme/js/file.js');
Then you can use it 'as normal'; for example, if you have a function in file.js that is called "eatmybadger()", then you can assign it either using jQuery or hard-code into the HTML like so:-
<img onclick='eatmybadger();' src='' .....
As for the other questions on adding an ID to your image, well, it all depends on how you get the image in the first place.
For my galleries, I have a simple PHP loop embedded into the page--xxxxtpl.php page which looks something like this:-
forech ($imageset as $image_id => $image_data) {
echo '<img
onclick="
eatmybadger(' . $image_data -> image_filename . ');
"
src="' . $image_data -> image_source . '"
><br />';
}
I hope this helps!
Adding the script in
Jaypan commented
Adding the script in page.tpl.php will include the script on every page. The question at hand is how to include the script on a single page.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Sorry, wasn't being clear ...
badger_fruit commented
Sorry, wasn't being clear ... I don't mean include it on page.tpl.php but on his appropriate template page; for example on my configuration, I have a page called page--archive-gallery.tpl.php and the drupal_add_js function that I suggested is used on that page; the code included in my JS file is then only available/called on that page, which is what the OP desires.
Why can't Meta Tags do this?
broadway commented
The Meta Tags module tucks all sorts of things in the head of a single node. (Title, description, canonical, rel="prev", etc...)
Why can't that same module just be configured to offer a feature where there's an input box where you can place any text you want for insertion into the head portion of the html, with you just being responsible for it having correct syntax? So much about Drupal is like doing your job handcuffed. Yeah, you can do it, it's just incredibly inconvenient.
https://www.drupal.org/node/1993228 这个页面也有不错的方法
This user never replied to my
Jaypan commented
This user never replied to my question, and since I'm sure that this page will be found by others searching for the same question, I'm going to give a few examples based on different page generation methods that I listed above:
1) A node:
Solution - implement hook_node_view() and add your script to the $node->content array as follows:
2) A template. For example, the user may have page--front.tpl.php.
Solution - We need to implement the preprocess hook for the template, in our template.php file. The actual function name will be THEME_NAME_preprocess_TEMPLATE(). In the above case, the template is 'page', so we want to implement THEME_NAME_preprocess_page() as follows:
3) A custom page created with hook_menu():
Solution - when creating a page with a callback path, using hook_menu(), it's best to return a render array, so that the page can be overridden further down the line. We can also attach our script to this render array. Example:
4) A view
Solution - implement hook_preprocess_views_view() and attach the js for your view and view name. In this hook, the view information is stored in $vars['view'], and we want to look at two items in particular, the view name, and the view current_display. The view name is the name of the overall view, when creating a new view, as it's listed on the views list page. The current_display is the name of the display being used inside that view. By default, it will be page_X (for a page) where X is the number of the page created. I generally override the name of the current_view however to something more descriptive. But I will use page_1 in this example:
I have attached a file in each of the above examples, but in all cases, I could have attached some settings, an external script, or inline JS as well.
Think the Drupal forums suck? Add your agreement here:https://www.drupal.org/node/2641072
Log in or register to post comments