SPECIAL OFFER: Only $69 for access to everything in OSTraining for 1 year! You save $75!  1
Join today and get access to 1,000's of books and videos. Learn WordPress, Drupal, Magento, Joomla and more! Sign up today!

loading jQuery

4 years 9 months ago - 4 years 9 months ago #119425 by Juc2
loading jQuery was created by Juc2
Hi all,

Is there any tutorial / link explaining jQuery in Drupal 8? eg what version is used in core, where the file is, is it activated by default or do I need to activate it somewhere?

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119438 by steve
Replied by steve on topic loading jQuery
Hi Juc2

Not yet, I don't believe, but that's a great idea for a tutorial.

It looks like the plan is to always ship Drupal with the very latest jQuery release: www.drupal.org/node/2607768

Please Log in to join the conversation.

4 years 9 months ago #119443 by Juc2
Replied by Juc2 on topic loading jQuery

steve wrote: Hi Juc2

Not yet, I don't believe, but that's a great idea for a tutorial.

It looks like the plan is to always ship Drupal with the very latest jQuery release: www.drupal.org/node/2607768


@ Steve I have found the jQuery file in core (screenshot below) but when I look at "view page source" in my D8 test site I can see in that jQuery is not loading - is there something I need to do to activate it?

Thanks...

This message has an attachment image.
Please log in or register to see it.

Please Log in to join the conversation.

4 years 9 months ago - 4 years 9 months ago #119449 by steve
Replied by steve on topic loading jQuery
Hi Juc2

That's odd - jQuery should be loaded via the Drupal core on every page.

Is there actually a link to a jQuery file in the source code?

Please Log in to join the conversation.

4 years 9 months ago #119454 by Juc2
Replied by Juc2 on topic loading jQuery

steve wrote: Hi Juc2

That's good. Hopefully jQuery will only load when needed.

I'd be disappointed if it was loaded on every page.


@ Steve ok so I want to add a snippet of code that depends on jQuery - rewinding to D7 for now, how would I load the jquery.min.js file in D7?

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119492 by Nick
Replied by Nick on topic loading jQuery
Hi Juc2,

Key questions:

1) Where specifically do you want it added? In your theme or somewhere else?

2) Also, do you want that snippet of code to appear on all pages or just some pages?

Looking forward to helping.

Kind regards,
Nick

Follow us on Twitter - twitter.com/OSTraining
Like us on Facebook - facebook.com/ostraining

Please Log in to join the conversation.

4 years 9 months ago - 4 years 9 months ago #119498 by Juc2
Replied by Juc2 on topic loading jQuery
@ Nick sticking to D7 for now, I want to apply my snippet of jQuery which targets the html on my home page. I know how to add a snippet of code to all pages using mytheme/js/example.js but I don't know how to add it to just one page. Also I know how to add an external js file to all pages - either copy to mytheme/js or add the source link to html.tpl.php - but I don't know how to add it to just one page. If the file I want to load is jquery.min.js this is already part of Drupal so I don't think the mytheme/js or html.tpl.php methods would be correct, or I mean I think I should be loading the jquery.min.js that is part of Drupal not some other / second jquery.min.js

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119538 by steve
Replied by steve on topic loading jQuery
Hi Juc2

This offers a pretty good explanation:
renaudjoubert.com/en/article/how-to-add-...drupal-7-with-jquery

I tried this exact same method in Drupal 7 but by default the script tags and its content are removed (deleted) by the system for security reasons. There are probably ways to bypass this but it's bad practice.


So they recommend using a custom template file. There is some useful follow-up in the comments on that post too.

Please Log in to join the conversation.

4 years 9 months ago - 4 years 9 months ago #119685 by Juc2
Replied by Juc2 on topic loading jQuery

steve wrote: Hi Juc2

That's good. Hopefully jQuery will only load when needed.

I'd be disappointed if it was loaded on every page.


Here is a new / clean D7 site test and you can see in view page source that for the default home page and the only node (where the content is single image) it looks like jQuery is loading =

This image is hidden for guests.
Please log in or register to see it.



Any ideas what is causing jQuery to load?

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119687 by steve
Replied by steve on topic loading jQuery
Hi Juc2

Can you explain that paste a little more?

I opened it up and saw this lin: desertfrog.co.uk/misc/jquery.js?v=1.4.4 It looks like the jQuery file is valid on the site.

Please Log in to join the conversation.

4 years 9 months ago #119690 by Juc2
Replied by Juc2 on topic loading jQuery

steve wrote: Hi Juc2

Can you explain that paste a little more?

I opened it up and saw this lin: desertfrog.co.uk/misc/jquery.js?v=1.4.4 It looks like the jQuery file is valid on the site.


@ Steve please see my edited post above = #119685

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119694 by steve
Replied by steve on topic loading jQuery
Hi Juc2

The $head variable is produced by the core drupal theme layer, in includes/theme.inc

www.drupal.org/node/759076

Please Log in to join the conversation.

4 years 9 months ago #119748 by Juc2
Replied by Juc2 on topic loading jQuery

steve wrote: Hi Juc2

The $head variable is produced by the core drupal theme layer, in includes/theme.inc

www.drupal.org/node/759076


@ Steve that link is talking about D6 - does it still apply for D7? (I can't see jquery mentioned in D7's includes/theme.inc).

Thanks...

Please Log in to join the conversation.

4 years 9 months ago #119774 by Valentin
Replied by Valentin on topic loading jQuery
Hi Juc2,
You're right!

About your original question:

Any ideas what is causing jQuery to load?

jQuery is loaded by default in Bartik theme.

Kind regards,
Valentín

Follow us on Twitter - twitter.com/OSTraining
Like us on Facebook - facebook.com/ostraining

Please Log in to join the conversation.

Join today and get access to 1,000's of books and videos. Learn WordPress, Drupal, Magento, Joomla and more! Sign up today!