adding external javascript content to your drupal nodes or views
posted september 30th, 2007 by john
sometimes you want to include jquery / javascript code in content areas in your nodes, views etc. it's arguable that this code is better in managed in the filesystem (under source code control) than in your database-stored-node. here's how you might do it using a php include.
and find yourself golden.
an example node
let's assume that the content of your node is:<?php
drupal_add_js(
'$(document).ready(function()
{
$("p").fadeIn("slow").addClass("error");
});',
'inline'
);
?>
<p>super paragraph</p>create a javascript file in the drupal directory for your site
create a file called e.g.sites/default/js/slowFadeIn.js with contents:
<?php
drupal_add_js(
'$(document).ready(function()
{
$("p").fadeIn("slow").addClass("error");
});',
'inline'
);
?>include the code from your node
your final node will look like:<?php include('sites/default/js/slowFadeIn.js'); ?>
<p>super paragraph</p>an exercise
this approach mandates that the input format for the text isphp code. it would be nice to add a filter to do a enumerated include (only a certain set of approved files), to allow the inclusion of pre-approved code on random nodes, views using a friendlier input format e.g. filtered html.
- john's blog
- 2337 reads
Please note, this entry has been closed to new comments.





delicious
digg
reddit
google
yahoo