Hello,

I was doing some research but honestly I am not that experienced with programming and the drupal structure.
I know there is a way of adding a javascript into EVERY page's header.

My question is, that I want to add it just into specific content type pages?
Furthermore: I did not really get, how to add the javascript so that it will be placed into each page.

Following this source: Here
I just need to create that Javascript File, and add the line into the used templates info file?

I think, before this line I would have to implement the request about the node type, isnt it?
So that he just writes the script into those specific notes?

Thank you very much in advance,
Björn

Comments

jaypan’s picture

You can attach the script in hook_node_view():

function mymodule_node_view($node, $view_mode)
{
  $node->content['#attached']['js'][] = array
  (
    'type' => 'file',
    'data' => drupal_get_path('module', 'mymodule') . '/my_js_file.js',
  );
}

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
dafreekzy’s picture

Heyy Jaypan - thank you very much.

I am still not sure (in the drupal folder) into which document should I put that code?

I mean - where can I find the "hook_node_view()" ? And in your code --> Do I need to replace only the "mymodule" and "/_my_js_file.js"? Or also the other red things?

Thanks a lot - it sucks.. in this kind I do not know anything, but I alrady learned a lot within 2 weeks in my drupal.. ;)

Thanks for ur support so far!

jaypan’s picture

You'll need to create a custom module for this. First read this section: https://drupal.org/node/292

Then this one: https://drupal.org/developing/modules/7


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.