I have a custom content type called 'Visualisation'. I would like a user to be able to upload their own javascript file for each instance of the content type. I would then like to load this script on the page in which the content is displayed.

I have tried adding a custom field 'file', however Drupal doesn't allow javascript files and appends the file with 'txt'. How can I add a javascript file and then load when the content is displayed?


As it seems unsafe to achieve this the way I've mentioned above I'll try and elaborate on what I'm trying to do.

The content type is for HTML5 canvas visualisations. Each visualisation will use a custom created script file. The whole content will consist of a title, canvas element, javascript file and description. I therefore need to attach a script to each visualisation.

Would it be better to have a field with just the name of the file and load it in the page template?

Many thanks for any help.

  • 1
    You may want to think of a different approach. This could open you up to cross site scripting attacks. Perhaps expand out your question a bit more to let the community advise you on alternate options.
    – Rick
    Commented Dec 1, 2014 at 16:26
  • Out of curiosity, what will the js files look like?
    – Jance
    Commented Dec 1, 2014 at 16:46
  • in this instance they'd be d3.js scripts but maybe not in every instance
    – RobotEyes
    Commented Dec 1, 2014 at 16:51

1 Answer 1


First things first: a warning. This sounds like an extremely dangerous idea. If you do this, please at least make sure that you're only opening this up to trusted users (as allowing people to place javascript on your page could lead to some very, very bad things, much like the php filter.)

That being said, if your sure you want to do this, your gonna need to add the js to your page programmaticly. You could easily do this through your theme functions of template_preprocess_node.

Find that function in your template.php file. You'll want to add a condition to check for the content type, then grab the js file and use drupal_add_js to grab the attached js file and attach it to the head.

  • Ah. Is there a safer/better way to add an individual javascript file for each instance of content? Maybe pulling a javascript file from the libraries folder with the same name as the content title perhaps?
    – RobotEyes
    Commented Dec 1, 2014 at 16:31
  • 1
    Providing a limited number of js choices for the user might be a smart way to do it. Say you have 30 js files that your going to use stored in libraries or in the files folder, you could provide a drop down on the content type that would contain the js name, and then just go to the folder and pull it that way. Idea is, as Rich pointed out, you want to limit the chances for a cross site scripting attack as much as possible. Execution would be similar, but instead of pulling the file location from the node, you would just get it from known-location/+filename+.js
    – Jance
    Commented Dec 1, 2014 at 16:38
  • thanks for the help. So I just need a field with the name of the file and then I can pull the file from a set location.
    – RobotEyes
    Commented Dec 1, 2014 at 16:42
  • Yeah, as long as you know where your files will be, that should be fine.
    – Jance
    Commented Dec 1, 2014 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.