I have a jQuery plugin that I authored and I wan开发者_StackOverflow社区t it on only one node of my Drupal 6 web site. I have FTP and a Linux shell I can use for uploading files to the site, but the issue is actually attaching them to a node. Putting JavaScript in a node's Body can get pretty ugly, especially when the Body is presented in a WYSIWYG editor (haywire indentation, WYSIWYG attempting to wrap all my <script> tags with <p> tags, etc)
Is there any sort of Drupal plugin or any kind of workaround to make this kind of integration easier?
Edit:
I've tried Code Per Node, which is great for separating JavaScript from the Body, but I really need the option to link to separate JavaScript files (this plugin requires several support files).
The way I did this in a project was to add a CCK text field to the content type called "Additional Resources" that accepts multiple values. Then in the node I added multiple values in this field -- the paths to CSS and JS files I wanted to load on that node. Then, in my theme I added my own function called themename_node_process_fields
in template.php
. That function was the first thing executed in node-content_type_name.tpl.php
. Among other things, it did this:
// Loop through the additional resources and add them to the <head>.
if (isset($node->field_additional_resources) && count($node->field_additional_resources) > 0) {
foreach ($node->field_additional_resources as $resource) {
if (strpos($resource['safe'], '.css') !== FALSE)
drupal_add_css($resource['safe'], 'theme', 'all', FALSE);
else if (strpos($resource['safe'], '.js') !== FALSE)
drupal_add_js($resource['safe'], 'theme', 'header', FALSE, TRUE, FALSE);
}
}
Note that I have not thought through if there are any security considerations for this. I'm the only one that has access to that field, but if a malicious user were able to enter arbitrary text I'm not sure what could be done.
If you aren't looking for an administrative tool, you can use drupal_add_js
function. It will insert the JavaScript file into the page at render time. However, this requires having the PHP input filter activated.
...content...
<?php
drupal_add_js('external_file.js', 'external');
?>
...more content...
From the link:
External: Add external JavaScript ('external'): Allows the inclusion of external JavaScript files that are not hosted on the local server. Note that these external JavaScript references do not get aggregated when preprocessing is on.
The best way would be to make your own module that uses the plugin. That is the "Drupal" way to do it. You can also you drupal_add_js to attach scripts to the module. If it is only a specific node you need to attach this plugin to I would either make a if statement in the template looking for that node, kinda an ugly way to do it though.
精彩评论