Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-20 18:11:29

obley
New Member
From: Champaign, IL
Registered: 2006-04-07
Posts: 7
Website

loading jquery and general js practices

I’ve scanned around the forums and textbook and haven’t really found the answer. If I’ve missed it, please point me to a link.

Do I need to do anything special to get jquery to load? I know I can easily just <script type="text/javascript" src="<txp:site_url />/textpattern/jquery.js"></script>
but is there a preferred, special textpattern, method to call it up? If I don’t manually, call it up, I get “$ is not defined”

oh, and this is a clean install of txp 4.0.5 and I’ve updated to the latest release of jquery.js

Finally, I’d like to hear how people like to pop in their custom javascript. I’ve created a form and stick it all there, calling it on the pages I need it. I think that is in the spirit of textpattern, no? Anyone actually make js library files?

thanks

Offline

#2 2008-01-20 18:20:56

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: loading jquery and general js practices

The stm_javascript plugin is very useful if you are working with JS. The way you load JQuery is fine. There really isn’t a better way that I know of.

Offline

#3 2008-01-20 18:56:16

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: loading jquery and general js practices

Your current code should be <txp:site_url />textpattern/jquery.js because site_url already outputs a forward slash.

For me, I externalize all of my JavaScript and call it with <txp:site_url />lib/js/file_name.js. I then load the js for whatever page with:

// site.load is a prefixed addLoadEvent
site.load(function() {
	switch(document.body.getAttribute('id')) {
		case 'articles':
			site.doStuff();
			site.doMoreStuff();
		break;

		default:
			site.search('q', 'Enter search terms');
	};
});

I find it easier to keep all JS, even minimal templating logic, in the js file.

Last edited by jm (2008-01-20 18:57:54)

Offline

#4 2008-01-21 14:36:19

obley
New Member
From: Champaign, IL
Registered: 2006-04-07
Posts: 7
Website

Re: loading jquery and general js practices

great. Thanks fellas

Offline

#5 2008-02-19 07:21:38

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: loading jquery and general js practices

So, is better to load the default TXP jquery with the stm_javascript plugin? Or is the same?

Offline

#6 2008-02-19 07:44:57

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

Re: loading jquery and general js practices

no its not better. its slower. although theres nothing wrong with the stm_javascript method, personally i think all js files should be external files. same with css (thanks to rvm_css)

Last edited by iblastoff (2008-02-19 07:45:16)

Offline

Board footer

Powered by FluxBB