Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-02-02 20:22:59

Nuggs
New Member
From: Tacoma, WA
Registered: 2012-02-02
Posts: 6

Javascript loading[solved]

First off, hello everyone! :D

Anywho, I’ve grown accustom to loading my JS last. I’ve noticed that with one of the plugins(cbs_live_search) I want to use, when I place my code for loading JS at the bottom, the plugins don’t seem to work.

The real question is, am I going to have to get used to placing my JS loading in the head or even using a defer when in use with TXP?

If this was asked, I’m sorry but my searches didn’t really come up with the question I had, just about external sources.

Last edited by Nuggs (2012-02-02 21:29:01)

Offline

#2 2012-02-02 20:26:38

tom1
Member
Registered: 2009-03-20
Posts: 36

Re: Javascript loading[solved]

You can always write your JS on its own form, where its easily accessible. Also, its easy and simple to include it in every page in head section with <txp:output_form /> – tag!

Offline

#3 2012-02-02 20:47:42

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,948
Website GitHub

Re: Javascript loading[solved]

Nuggs wrote:

am I going to have to get used to placing my JS loading in the head or even using a defer when in use with TXP?

I think it depends on the plugins you want to use. In this case, cbs_live_search stipulates that you need to load jQuery first so that’s what you should do. Some plugins won’t mind either way but I’ve found things work best with jQuery loaded in the <head> ; everything else (analytics, piwik, plugin stuff) can usually wait until </body>.

I’m not quite sure what exactly constitutes “DOMReady” — it might be when all the markup is known or it might be when scripts have finished loading. I suspect the former, so I guess loading jQuery first gives it more time to get across the wire and be prepared for any plugins that might kick off the moment the </html> is encountered.

It’s entirely possible I’m way off base here with how it works — it’s not something I know much about — so we’ll wait until a guru can shed more light on it. In the meantime I’m just throwing this out there because that’s what I’ve found works most reliably with Textpattern sites.

Hope that help a bit.

EDIT: and to echo what tom1 said, I have two Textpattern forms that I employ in most of my sites: head_scripts and tail_scripts. One is inserted (using <txp:output_form form="head_scripts" />) in the head and the other Form is called right at the end, just before the closing body tag. That way I can pick and choose which scripts / code get executed where and can tweak things if some stuff doesn’t work right with it loaded at the end.

Last edited by Bloke (2012-02-02 20:52:19)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2012-02-02 21:24:57

Nuggs
New Member
From: Tacoma, WA
Registered: 2012-02-02
Posts: 6

Re: Javascript loading[solved]

Thanks for the input, I must have missed that when I was reading over cbs_live_search.

Both of you have good information. I believe I’ll just load the mandatory scripts in the head and use another for the not so important.

I’ve been kind of out of the web game for quite a few years as my main expertise is writing C on UNIX platforms and server hardware.

I think I can handle loading jquery in the head, it doesn’t cause any loading issues, I just was thinking down the line I don’t want my users who haven’t cached the site to have to make tons of http requests before the page has even loaded.

Thanks again for the tips and info guys. I think I have a good idea of how I’m going to be handling my JS loading now. :D

Last edited by Nuggs (2012-02-02 21:28:30)

Offline

#5 2012-02-02 22:08:11

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Javascript loading[solved]

As a side note, you can load jquery from google CDN in your head section, that way if a user has already cached jquery that way from another site using the same method, they will avoid the extra http request.

Offline

#6 2012-02-03 02:03:38

Nuggs
New Member
From: Tacoma, WA
Registered: 2012-02-02
Posts: 6

Re: Javascript loading[solved]

philwareham wrote:

As a side note, you can load jquery from google CDN in your head section, that way if a user has already cached jquery that way from another site using the same method, they will avoid the extra http request.

Good call, I was just about to do some reading up on the Google CDN. :D

This forum has so many helpful users! :)

Offline

Board footer

Powered by FluxBB